简体   繁体   English

static Object.Equals方法,GetHashCode的默认实现和Dictionary类

[英]static Object.Equals method, default implementation of GetHashCode and the Dictionary class

I just want to confirm my understanding of a few fundamentals. 我只是想确认一下我对一些基本原理的理解。 Hope you don't mind! 希望你不要介意!

I understand the static equals method 我理解静态等于方法

Object.Equals(objA, objB)

first checks for reference equality. 首先检查参考相等性。 If not equal by reference, then calls the object instance equals method 如果不等于引用,则调用对象实例equals方法

objA.Equals(objB)

Currently in my override for equals, i first check for reference equality, and if not equal referentially then check with all members to see if the semantics are the same. 目前在我的equals覆盖中,我首先检查引用相等性,如果不等于引用,则检查所有成员以查看语义是否相同。 Is this a good approach? 这是一个好方法吗? If so, then the static version seems superfluous? 如果是这样,那么静态版本似乎是多余的?

Also what exactly does the default GetHashCode for an object do? 另外,对象的默认GetHashCode究竟是做什么的呢?

If I add my object to a dictionary which is a HashTable underneath and don't override equals and GetHashCode, then I guess I should do to make it sort optimally hence better retrieval time? 如果我将我的对象添加到下面是HashTable的字典并且不覆盖equals和GetHashCode,那么我想我应该做的是使它最佳排序因此更好的检索时间?

Currently in my override for equals, i first check for reference equality, and if not equal referentially then check with all members to see if the semantics are the same. 目前在我的equals覆盖中,我首先检查引用相等性,如果不等于引用,则检查所有成员以查看语义是否相同。 Is this a good approach? 这是一个好方法吗? If so, then the static version seems superfluous? 如果是这样,那么静态版本似乎是多余的?

Yes, it's a great idea to do the fast reference-equality check. 是的,进行快速参考等式检查是个好主意。 There's no guarantee that your method will be called through the static Object.Equals method - it could well be called directly. 无法保证您的方法将通过静态Object.Equals方法调用 - 它可以直接调用。 For example, EqualityComparer<T>.Default (the typical middleman for equality checking) will directly call this method in many situations (when the type does not implement IEquatable<T> ) without first doing a reference-equality check. 例如, EqualityComparer<T>.Default (用于相等性检查的典型中间人)将在许多情况下(当类型未实现IEquatable<T> )直接调用此方法,而不首先执行引用相等性检查。

Also what exactly does the default GetHashCode for an object do? 另外,对象的默认GetHashCode究竟是做什么的呢?

It forwards to RuntimeHelpers.GetHashCode : a magic, internally-implemented CLR method that is a compliant GetHashCode implementation for reference-equality. 它转发到RuntimeHelpers.GetHashCode :一个神奇的,内部实现的CLR方法,它是一个兼容的GetHashCode实现,用于引用相等。 For more information, see Default implementation for Object.GetHashCode() . 有关更多信息,请参阅Object.GetHashCode()的默认实现 You should definitely override it whenever you override Equals . 每当你重写Equals时,你一定要覆盖它。

EDIT: 编辑:

If I add my object to a dictionary which is a HashTable underneath and don't override equals and GetHashCode, then I guess I should do to make it sort optimally hence better retrieval time? 如果我将我的对象添加到下面是HashTable的字典并且不覆盖equals和GetHashCode,那么我想我应该做的是使它最佳排序因此更好的检索时间?

If you don't override either, you'll get reference-equality with (probably) a well-balanced table. 如果你不重写,你将获得与(可能)一个平衡良好的表的引用相等。 If you override one but not the other or implement them in any other non-compliant way, you'll get a broken hashtable. 如果你覆盖一个而不是另一个,或者以任何其他不合规的方式实现它们,你将获得一个破碎的哈希表。

By the way, hashing is quite different from sorting. 顺便说一下, 散列与排序有很大不同。

For more information, see Why is it important to override GetHashCode when Equals method is overriden in C#? 有关更多信息,请参阅在C#中覆盖Equals方法时为什么重写GetHashCode很重要?

Your first question was already answered, but I think the second was not fully answered. 你的第一个问题已经回答,但我认为第二个问题没有得到完全回答。

Implementing your GetHashCode is important if you want to use your object as a key in a hash table or a dictionary. 如果要将对象用作哈希表或字典中的键,则实现GetHashCode非常重要。 It minimizes collisions and therefore it speeds the lookup. 它最大限度地减少了碰撞,因此加快了查找速度。 A lookup collision happens when two or more keys have the same hashcode and for those equals method is invoked. 当两个或多个键具有相同的哈希码时会发生查找冲突,并且对于那些调用equals方法。 If the hashcode is unique, an equals will only be called once, otherwise it will be called for every key with the same hashcode until the equals returns true. 如果哈希码是唯一的,则equals将只调用一次,否则将为具有相同哈希码的每个键调用它,直到equals返回true。

暂无
暂无

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

相关问题 覆盖Equals和GetHashCode-派生类中的默认实现 - Overriding Equals and GetHashCode - default implementation in derived class Object.Equals的奇怪实现 - Strange implementation of Object.Equals Object.Equals:默认情况下一切都是相同的 - Object.Equals: everything is equal by default 字典C中的GetHashCode和Equals实现# - GetHashCode and Equals implementation in Dictionary C# 平等比较者<T> .Default.Equals() 与 object.Equals() 和多态 - EqualityComparerer<T>.Default.Equals() vs object.Equals() and polymorphism GetHashCode等于C#中的类的实现 - GetHashCode Equals implementation for a class in C# C#运算符重载:Object.Equals(object o)和Object.GetHashCode() - C# operator overloading: Object.Equals(object o) & Object.GetHashCode() 警告:“...覆盖Object.Equals(对象o)但不覆盖Object.GetHashCode()” - Warning: “… overrides Object.Equals(object o) but does not override Object.GetHashCode()” 为什么我收到此Resharper警告-不覆盖&#39;Object.Equals(object o)和&#39;Object.GetHashcode()&#39; - Why I am getting this Resharper warning - does not override 'Object.Equals(object o) and 'Object.GetHashcode()' 覆盖C#中的Object.Equals()实例方法;现在代码分析/ FxCop警告CA2218:“还应该重新定义GetHashCode”。我该抑制吗? - Overriding Object.Equals() instance method in C#; now Code Analysis/FxCop warning CA2218: “should also redefine GetHashCode”. Should I suppress this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM