C# 理解 IEquatable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/411500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:20:28  来源:igfitidea点击:

Understanding IEquatable

c#iequatable

提问by leora

When I implement objects that I want to compare using the IEquatable<T>interface:

当我使用IEquatable<T>接口实现我想比较的对象时:

  1. Why do I have to override Equals(object)method if I already implemented Equals(T)?
  2. Can I use ==and !=operators once I implement IEquatable<T>?
  1. Equals(object)如果我已经实现,为什么我必须覆盖方法Equals(T)
  2. 实现后可以使用==!=运算符IEquatable<T>吗?

采纳答案by Ray Booysen

  1. From MS Docs article on IEquatable<T>:

    If you implement IEquatable<T>, you should also override the base class implementations of Equals(Object)and GetHashCode()so that their behavior is consistent with that of the Equals(T)method. If you do override Equals(Object), your overridden implementation is also called in calls to the static Equals(Object, Object)method on your class. In addition, you should overload the op_Equalityand op_Inequalityoperators. This ensures that all tests for equality return consistent results.

  2. No, operators do not use the Equals method. They must be overloaded separatelyto do so.

  1. 来自MS Docs 文章IEquatable<T>

    如果实现了IEquatable<T>,你也应该重写的基类的实现 Equals(Object),并 GetHashCode()让他们的行为与该一致的 Equals(T)方法。如果您执行 override Equals(Object),则在Equals(Object, Object)对类上的静态方法的调用中也会调用被覆盖的实现 。此外,您应该重载op_Equalityandop_Inequality运算符。这可确保所有相等性测试都返回一致的结果。

  2. 不,运算符不使用 Equals 方法。它们必须单独重载才能这样做。

回答by Jon Skeet

1) As Ray said, override Equals(object)to ensure consistency when the method is called from classes which don't know (statically) that you implement IEquatable<T>. For instance, the non-generic collections classes will use Equals(object)for comparisons. You should also override GetHashCode().

1)正如Ray所说,Equals(object)当从不知道(静态)实现的类调用方法时,重写以确保一致性IEquatable<T>。例如,非泛型集合类将Equals(object)用于比较。您还应该覆盖GetHashCode().

2) Implementing IEquatable<T>doesn't overload the == and != operators automatically, but there's nothing to stop you from doing so, just like System.Stringdoes. You should document this very clearly if you do, however - and be careful when you make comparisons between other types of reference (e.g. MyType and Object) which will still use the identity comparison. I suspect it's not a great idea to do this unless it's going to be a very heavily used type in your code, where everyone will become very familiar with it andwhere the syntactic sugar of overloading == will really make a positive impact on readability.

2) 实现IEquatable<T>不会自动重载 == 和 != 运算符,但是没有什么可以阻止您这样做,就像System.String做的那样。但是,如果这样做,您应该非常清楚地记录这一点 - 并且在将仍使用标识比较的其他类型的引用(例如 MyType 和 Object)之间进行比较时要小心。我怀疑这样做不是一个好主意,除非它在您的代码中成为一种非常频繁使用的类型,每个人都会非常熟悉它,并且重载 == 的语法糖将真正对可读性产生积极影响。