gpt4 book ai didi

.net - Enumerable.SequenceEqual 和 EqualityComparer

转载 作者:行者123 更新时间:2023-12-01 13:01:28 29 4
gpt4 key购买 nike

来自 MSDN

The SequenceEqual(IEnumerable, IEnumerable) method enumerates the two source sequences in parallel and compares corresponding elements by using the default equality comparer for TSource, Default. The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface.

a) 据我理解上面的引用,它暗示 EqualityComparer<T>.Default用于比较实现 IEqualityComparer<T> 的类型的元素, 事实上 Default用于返回 IEqualityComparer<T> 的特定实现要么调用IEquatable<T>.Equals (假设 T 可分配给 IEquatable<T> )或者它调用 Object.Equals

b) 引用还表明 TSource必须实现 IEqualityComparer<T> ,这是不正确的:

   static void Main(string[] args)
{
Test test1 = new Test();
Test test2 = new Test();

Test[] list1 = { test1, test2 };
Test[] list2 = { test1, test2 };

bool eq = list1.SequenceEqual(list2); //works
}

public class Test { }

那么我是不是误解了这句话试图传达的意思,或者这句话明显是错误的?

谢谢

最佳答案

不是TSource应该实现 IEqualityComparer

但是EqualityComparer<T>.Default工具 IEqualityComparer

The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface.

这太容易忘记了。

关于.net - Enumerable.SequenceEqual<TSource> 和 EqualityComparer<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5721271/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com