使用Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)判断两个集合是否相同
来源:互联网 发布:java fastjson api 编辑:程序博客网 时间:2024/05/29 06:28
Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)
- .NET Framework 4
- .NET Framework 3.5
- Silverlight
通过使用相应类型的默认相等比较器对序列的元素进行比较,以确定两个序列是否相等。
程序集: System.Core(在 System.Core.dll 中)
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second)
类型参数
- TSource
输入序列中的元素的类型。
参数
- first
- 类型:System.Collections.Generic.IEnumerable<TSource>
一个用于比较 second 的IEnumerable<T>。
- second
- 类型:System.Collections.Generic.IEnumerable<TSource>
一个 IEnumerable<T>,用于与第一个序列进行比较。
返回值
类型:System.Boolean如果根据相应类型的默认相等比较器,两个源序列的长度相等,且其相应元素相等,则为true;否则为 false。
使用说明
在 Visual Basic 和 C# 中,可以在 IEnumerable<TSource> 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见扩展方法 (Visual Basic)或扩展方法(C# 编程指南)。first 或 second 为 null。
SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 方法枚举两个并行源序列,并使用用于 TSource, Default 的默认相等比较器对相应的元素进行比较。 默认相等比较器 Default 用于比较实现了IEqualityComparer<T> 泛型接口的类型的值。 若要比较自定义类型,需要为该类型实现此接口并提供自己的 GetHashCode 和Equals 方法。
下面的代码示例演示如何使用 SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 确定两个序列是否相等。 在前两个示例中,方法决定比较的序列是否包含对同一对象的引用。 在第三个和第四个示例中,方法比较序列内对象的实际数据。
在本示例中,这两个序列相等。
class Pet { public string Name { get; set; } public int Age { get; set; } } public static void SequenceEqualEx1() { Pet pet1 = new Pet { Name = "Turbo", Age = 2 }; Pet pet2 = new Pet { Name = "Peanut", Age = 8 }; // Create two lists of pets. List<Pet> pets1 = new List<Pet> { pet1, pet2 }; List<Pet> pets2 = new List<Pet> { pet1, pet2 }; bool equal = pets1.SequenceEqual(pets2); Console.WriteLine( "The lists {0} equal.", equal ? "are" : "are not"); } /* This code produces the following output: The lists are equal. */
下面的代码示例比较两个不相等的序列。 请注意,序列包含完全相同的数据,但因为它们包含的对象具有不同的引用,该序列不会被视为相等。
class Pet { public string Name { get; set; } public int Age { get; set; } } public static void SequenceEqualEx2() { Pet pet1 = new Pet() { Name = "Turbo", Age = 2 }; Pet pet2 = new Pet() { Name = "Peanut", Age = 8 }; // Create two lists of pets. List<Pet> pets1 = new List<Pet> { pet1, pet2 }; List<Pet> pets2 = new List<Pet> { new Pet { Name = "Turbo", Age = 2 }, new Pet { Name = "Peanut", Age = 8 } }; bool equal = pets1.SequenceEqual(pets2); Console.WriteLine("The lists {0} equal.", equal ? "are" : "are not"); } /* This code produces the following output: The lists are not equal. */
如果要比较实际的数据,而不是只比较它们的引用序列中的对象,则必须在您的类中实现 IEqualityComparer<T> 泛型接口。 下面的代码示例演示如何在自定义数据类型中实现此接口并提供 GetHashCode 和Equals 方法。
public class Product : IEquatable<Product>{ public string Name { get; set; } public int Code { get; set; } public bool Equals(Product other) { //Check whether the compared object is null. if (Object.ReferenceEquals(other, null)) return false; //Check whether the compared object references the same data. if (Object.ReferenceEquals(this, other)) return true; //Check whether the products' properties are equal. return Code.Equals(other.Code) && Name.Equals(other.Name); } // If Equals() returns true for a pair of objects // then GetHashCode() must return the same value for these objects. public override int GetHashCode() { //Get hash code for the Name field if it is not null. int hashProductName = Name == null ? 0 : Name.GetHashCode(); //Get hash code for the Code field. int hashProductCode = Code.GetHashCode(); //Calculate the hash code for the product. return hashProductName ^ hashProductCode; }}
实现此接口后,您可以使用 SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 方法中Product 对象的序列,如下面的示例所示。
Product[] storeA = { new Product { Name = "apple", Code = 9 }, new Product { Name = "orange", Code = 4 } }; Product[] storeB = { new Product { Name = "apple", Code = 9 }, new Product { Name = "orange", Code = 4 } }; bool equalAB = storeA.SequenceEqual(storeB); Console.WriteLine("Equal? " + equalAB); /* This code produces the following output: Equal? True */
- 使用Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)判断两个集合是否相同
- Enumerable.ToList(TSource) 方法
- Enumerable.ToList<TSource> 方法
- Enumerable.OfType<TResult> 方法 (IEnumerable)
- Func<TResult>和Func<TSource, TResult>
- Linq学习__07__差集Except、并集Union 和 Concat(TSource)
- IEnumerable
- Ienumerable
- [LINQ] IEnumerable接口 与 Enumerable扩展
- System.Linq.Enumerable类中的两个where方法都是对于IEnumerable的扩展方法,那LINQ是如何支持Array的
- IEnumerable接口使用
- 数组,集合,IEnumerable接口,迭代器
- 数组,集合,IEnumerable接口,迭代器
- 数组,集合,IEnumerable接口,迭代器
- C# IEnumerable集合转为DataTable
- IEnumerable IListIList,ICollection,IEnumerable
- 判定两个集合是否相同
- IEnumerable和IEnumerator的使用
- DM6437 VPFE和VPBE的中断机制
- 如何添加Samba用户
- 黑马程序员---交通灯管理系统
- 线段树成段跟新个人总结
- 黑马程序员———Java 8 最新版本
- 使用Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)判断两个集合是否相同
- Windows驱动_文件系统过滤驱动之三
- 计算机科学中最重要的32个算法
- <cstring>、<string.h>和<string>用法区别总结
- 黑马程序员———Eclipse快捷键大全
- Window 窗口属性及嵌入窗口到D3D渲染窗口顶层
- Linux内核的配置与编译(X86平台)
- 计算机中的端口
- MBA最全的面试训练