使用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.5             
其他版本                             
        
  • .NET Framework 4        
  • .NET Framework 3.5        
  • Silverlight        
此主题尚未评级- 评价此主题                        

通过使用相应类型的默认相等比较器对序列的元素进行比较,以确定两个序列是否相等。                      

命名空间:    System.Linq
程序集:    System.Core(在 System.Core.dll 中)
语法

C#
C++
F#
VB
复制
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second)

类型参数

TSource

输入序列中的元素的类型。

参数

first
类型:System.Collections.Generic.IEnumerable<TSource>
一个用于比较 secondIEnumerable<T>
second
类型:System.Collections.Generic.IEnumerable<TSource>
一个 IEnumerable<T>,用于与第一个序列进行比较。

返回值

类型:System.Boolean
如果根据相应类型的默认相等比较器,两个源序列的长度相等,且其相应元素相等,则为true;否则为 false

使用说明

在 Visual Basic 和 C# 中,可以在 IEnumerable<TSource> 类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见扩展方法 (Visual Basic)或扩展方法(C# 编程指南)。
异常

异常条件ArgumentNullException

firstsecondnull

备注

   SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)  方法枚举两个并行源序列,并使用用于 TSource, Default 的默认相等比较器对相应的元素进行比较。 默认相等比较器 Default 用于比较实现了IEqualityComparer<T> 泛型接口的类型的值。 若要比较自定义类型,需要为该类型实现此接口并提供自己的 GetHashCodeEquals 方法。 

示例

下面的代码示例演示如何使用 SequenceEqual<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 确定两个序列是否相等。 在前两个示例中,方法决定比较的序列是否包含对同一对象的引用。  在第三个和第四个示例中,方法比较序列内对象的实际数据。 

在本示例中,这两个序列相等。

C#
VB
复制
            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.            */

下面的代码示例比较两个不相等的序列。  请注意,序列包含完全相同的数据,但因为它们包含的对象具有不同的引用,该序列不会被视为相等。 

C#
VB
复制
            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> 泛型接口。 下面的代码示例演示如何在自定义数据类型中实现此接口并提供 GetHashCodeEquals 方法。 

C#
VB
复制
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 对象的序列,如下面的示例所示。

C#
VB
复制
        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        */