- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
摘自MSDN上 IEqualityComparer<T>
备注部分:
We recommend that you derive from the EqualityComparer<T> class instead of implementing the IEqualityComparer<T> interface, because the EqualityComparer<T> class tests for equality using the IEquatable<T>.Equals method instead of the Object.Equals method. ...
我不理解引用中关于我们为什么更愿意从 EqualityComparer<T>
类派生而不是实现 IEqualityComparer<T>
的论点。这意味着实现 IEqualityComparer<T>
的对象将使用 Object.Equals
测试相等性,但是当我们不想使用 IEqualityComparer<T>
或 Object.Equals
测试相等性时,实现 IEquatable<T>.Equals
不是全部意义所在吗?
这也意味着如果我们从 EqualityComparer<T>
派生,那么派生类将使用 IEquatable<T>.Equals
方法测试是否相等。同样,当我们不想使用 EqualityComparer<T>
或 Object.Equals
测试相等性时(因为 IEquatable<T>.Equals
已经使用 EqualityComparer<T>.Default
或 Object.Equals
进行测试),从 IEquatable<T>.Equals
派生的全部意义不是吗?
... This is consistent with the Contains, IndexOf, LastIndexOf, and Remove methods of the Dictionary<TKey, TValue> class and other generic collections.
我假设 .NET 库中的大多数集合通过调用 IEqualityComparer<T>
或 IEquatable<T>.Equals
测试元素的默认相等性(即当用户不向这些集合提供他们自己的自定义 Object.Equals
对象时) (取决于类型 T
的元素是否实现 IEquatable<T>
)通过 EqualityComparer<T>.Default
。
为什么这些集合(在测试默认相等性时)不直接调用 IEquatable<T>.Equals
或 Object.Equals
而不是通过 EqualityComparer<T>.Default
类?
最佳答案
关于您的第一个问题:
备注部分为 IEqualityComparer<T>
class 似乎并没有真正提供为什么您应该更喜欢从抽象类派生而不是接口(interface)的原因,这听起来更像是平等比较器接口(interface)首先存在的原因。它所说的实际上是无用的,它基本上描述了默认实现正在做什么。如果有的话,他们在这里提供的“推理”听起来更像是你的比较者可以做什么的指南,而与它实际做什么无关。
查看 EqualityComparer<T>
的公共(public)/ protected 接口(interface)类,只有一个可取之处,它实现了非泛型 IEqualityComparer
界面。我认为他们的意思是说他们建议从中派生,因为 EqualityComparer<T>
实际上实现了非泛型 IEqualityComparer
接口(interface),这样你的类就可以在需要非通用比较器的地方使用。
在 IComparer<T>
的备注部分确实更有意义:
We recommend that you derive from the
Comparer<T>
class instead of implementing theIComparer<T>
interface, because theComparer<T>
class provides an explicit interface implementation of theIComparer.Compare
method and theDefault
property that gets the default comparer for the object.
我怀疑它应该为 IEqualityComparer<T>
说类似的话但有些想法被混淆了,最终导致描述不完整。
关于你的第二个问题:
图书馆馆藏的主要目标是尽可能灵活。一种方法是允许通过提供 IComparer<T>
来比较对象中的自定义方法。或 IEqualityComparer<T>
做比较。在没有提供默认比较器的情况下获取默认比较器的实例比直接进行比较要容易得多。这些比较器反过来可以包含调用适当打包的适当比较所需的逻辑。
例如,默认比较器可以确定是否T
工具 IEquatable<T>
并调用IEquatable<T>.Equals
在物体上或以其他方式使用 Object.Equals
.最好封装在比较器中,而不是可能在集合代码中重复。
此外,如果他们想退回到调用 IEquatable<T>.Equals
直接地,他们必须在 T
上添加约束这将使这个电话成为可能。这样做会降低灵 active ,并抵消了首先提供比较器的好处。
关于c# - 更喜欢 EqualityComparer<T> 而不是 IEqualityComparer<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5707347/
我有一个 Person 类,并创建了一个派生自 EqualityComparer 的相等比较器类。然而默认的 EqualityComparer 不调用我的相等比较器的 Equals 函数 根据 MSD
我想比较两个嵌套对象列表。如果父对象 Id 不同和/或任何子对象 Id 或 Baz 属性不同,我想认为它们已更改。 我已经在下面实现了我自己版本的 Equals 和 GetHashCode,但是尽管使
阅读 C++ 模板第二版的第 22 章,我试图理解 EqualityComparable 特性的实现。但我无法理解编译器如何决定是否激活回退。 除此之外还有两个只声明了程序编译运行的函数。这对我来说很
我见过的例子: int[] a1 = { 1, 2, 3 }; int[] b1 = { 1, 2, 3 }; a1.Equals(b1) //false a1.Equals(b1,Equal
谁能给我解释一下下面的类声明有什么问题: private class PriorityQueueEntry : IComparer> where TPriori
假设有这个类: public class Foo { public int Id { get; set; } public int? NullableId { get; set; }
我有一个带有自定义比较器的类型字典(字典),因为我们要存储2个给定类型之间的关系(对于MVVM模式),并且我需要帮助以寻求一种使自定义EqualityComparer正常工作的方法。 做一些研究,我发
我们的代码库目前有以下 EqualityComparer。 public static IEnumerable Exclude(this IEnumerable first,
我需要使用 EqualityComparer 初始化静态 dictionary,因为我使用自定义类作为此 dictionary 中的键。当这个 dictionary 不是静态的时,我可以初始化它,但是
我读过这个post但它没有回答我的问题。 MSDN says : We recommend that you derive from the EqualityComparer(Of T) class
对于具有属性 A、B、C、D、StartDate 和 EndDate 的对象,如果我想实现任何两个对象相等的东西,如果它们具有相同的 A、B 和 C 以及重叠的日期范围,那将如何完成? 我试过像这样创
为什么这个简单的测试会失败? var dict1 = new Dictionary {{1, 15}}; var dict2 = new Dictionary {{1, 15}}; var eq
我希望使用指定的 EqualityComparer 创建的 HashSet 在 Remove 操作中使用该比较器。特别是因为 Contains 操作返回 true! 这是我使用的代码: public
我有一个类Person , 它实现了 IEquatable 中的 Equals() 方法(也覆盖了 Object.Equals 方法,让我们暂时忽略 GetHashcode() 方法) class P
我正在阅读 EqualityComparer.Default 的源代码并发现它并没有那么聪明。这是一个例子: enum MyEnum : int { A, B } EqualityComparer.D
考虑 T = 字符串。 我很好奇它是否使用类似这样的东西:typeof(EqualityComparer).GetInterface("IEqualityComparer"); 任何建议.. 最佳答案
除了 .NET Framework 中的错误之外,还有其他解释吗? EqualityComparer.Default.Equals()方法是说以下网址是相等的! http://books.google
我的问题会有一个 bool 值答案:是或否。不管是哪一个,有人能解释一下下面的代码是如何被 GNU-g++ 4.9.2 和 clang 3.5 编译的,而 GNU-g++ 5.1.1 不再接受它,声称
我正在尝试使用显式键类型对某些对象执行 Linq GroupBy。我没有通过 IEqualityComparer到 GroupBy,所以根据文档: The default equality compa
来自 MSDN The SequenceEqual(IEnumerable, IEnumerable) method enumerates the two source sequences in pa
我是一名优秀的程序员,十分优秀!