使用Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)判断两个集合是否相同

Enumerable.SequenceEqual<TSource> 方法 (IEnumerable<TSource>, IEnumerable<TSource>)

            .NET Framework 4.5             
此主题尚未评级 - 评价此主题                         

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

命名空间:     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

first secondnull

   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
        */

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值