简体   繁体   English

所有c#类应该实现Equals和GetHashCode吗?

[英]should all c# classes implement Equals and GetHashCode?

should all c# classes override Equals and GetHashCode? 是否所有c#类都重写Equals和GetHashCode? For correctness 为了正确

No, they already do. 不,他们已经做到了。

Whether you have to override them, is up to how it will be used. 是否必须覆盖它们,取决于它将如何使用。 In most cases, it is not needed. 在大多数情况下,不需要它。

All classes already inherit these methods from the base class, System.Object. 所有类都已从基类System.Object继承这些方法。

You can choose to override the methods in derived classes if you need to be able to compare two instances of an object beyond simple reference equality, otherwise it's not necessary. 如果需要能够比较对象的两个实例而不是简单的引用相等性,则可以选择覆盖派生类中的方法,否则就没有必要。

Remember, however, that if you choose to override one of them, you also need to override the other in order to ensure that Hashtables and dictionary keys, among other things, work properly with you derived class. 但请记住,如果您选择覆盖其中一个,则还需要覆盖另一个,以确保Hashtables和字典键(以及其他功能)与您的派生类一起正常工作。 The GetHashCode method needs to reflect the same logic as the Equals method. GetHashCode方法需要反映与Equals方法相同的逻辑。 See here for more explanations and examples: 有关更多说明和示例,请参见此处:

http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

and

http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx

All classes to inherit this from the System.Object . System.Object继承此类的所有类。

If you need to provide a specific Equals or GetHashCode for a class then you should override the methods in your classes. 如果需要为类提供特定的Equals或GetHashCode,则应覆盖类中的方法。 Otherwise just leave them.. 否则就离开他们..

http://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=VS.71).aspx http://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=VS.71).aspx

Maybe not all, but all classes that will be put into a some kind of bag (IList, ICollection, IDictionary, Hashset, etc.) and need some simple method to differentiate them (just think about Sort() , Contains() , BinarySearch() , etc.). 也许不是全部,但所有类都将被放入某种包(IList,ICollection,IDictionary,Hashset等)并需要一些简单的方法来区分它们(只需考虑Sort()Contains()BinarySearch()等)。

If you use a class that way you should definitely implement them correct . 如果你以这种方式使用一个类,你肯定应该正确地实现它们。

When you override Equals , basically. 当你重写Equals ,基本上。 When you want to provide a different idea of equality than simple reference equality. 当你想提供一个不同的平等概念而不是简单的引用平等。

String is a good example of this - two strings are equal (under a simple Equals call) if they represent the same sequence of characters. String就是一个很好的例子 - 如果它们代表相同的字符序列,则两个字符串相等(在简单的Equals调用下)。 The hash code reflects this, such that if two strings are equal they will have the same hash code. 哈希码反映了这一点,这样如果两个字符串相等,它们将具有相同的哈希码。 (The reverse isn't necessarily true - two unequal strings can have the same hash code, but it's unlikely.) (反过来不一定正确 - 两个不相等的字符串可以具有相同的哈希码,但不太可能。)

(Strings are tricky in other ways, mind you - there are lots of different ideas of equality based on culture and casing, but String.Equals just looks at the UTF-16 code points which make up the string, and compares them in the simplest conceivable fashion.) (字符串在其他方面很棘手,请注意 - 基于文化和套管有很多不同的平等概念,但String.Equals只是查看构成字符串的UTF-16代码点,并在最简单的情况下对它们进行比较可以想象的时尚。)

by Jon Skeet 作者:Jon Skeet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM