C#.Net中的Object.GetHashCode()方法 [英] Object.GetHashCode() Method in C#.Net

查看:106
本文介绍了C#.Net中的Object.GetHashCode()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net中对象的GetHashCode()的确切用法是什么?它与垃圾收集有什么关系吗?

What is the exact use of GetHashCode() of an object in .net? Does it have any relation with garbage collection?

推荐答案

它与垃圾收集无关,嗯,只不过是所有其他类型的成员。



此函数用于使对象能够作为 associative 集合中的索引类型,例如 System.Collections.Generic .Dictionary System.Collections.Generic.SortedDictionary System.Collections.Generic.SortedList 。这种集合允许通过任何类型的某种键快速找到对象;这种搜索的计算复杂度为O(1);也就是说,搜索的时间渐近并不取决于集合的大小。



集合数据结构将数据分类为(请参阅上面的链接)具有相同的哈希码值,这是搜索算法的关键。



参见:

http://www.sgi.com/tech/stl/HashedAssociativeContainer.html [ ^ ],

http://en.wikipedia.org/wiki/Hash_table [ ^ ],

http://en.wikipedia.org/wiki/Big_O_notation [ ^ ]。



无论何时定义类型并覆盖相等方法 System.Object.Equals ,您还需要覆盖 System.Object.GetHashCode 。请参阅:

http://msdn.microsoft.com/en -us / library / system.object.aspx [ ^ ]。



这很自然:你不能一直实现一个没有另一个保持类型的正确行为作为集合键。







逻辑上相等的对象应显示相等的哈希码值,不等的对象应返回哈希码的值很高的概率 - 这是类实现的主要要求。



你可能会感到困惑,你应该知道如何实现哈希码适用于您的课程。我会给你一个非常简单的食谱。考虑您的类有几个在equals实现中使用的字段。假设他们已经有哈希代码的实现,取所有哈希代码值并对它们进行二进制异或。将结果值作为组合对象的哈希码返回。就是这样。



对于家庭练习,请考虑为什么这个实现有效。 :-)



-SA
It is not related to garbage collection, well, no more than all other type members.

This function is used to make an object to be capable of serving as an index type in associative collections such as System.Collections.Generic.Dictionary, System.Collections.Generic.SortedDictionary or System.Collections.Generic.SortedList. Such collection allow for fast finding of an object by some key of any type; and the computational complexity of such search is O(1); that is, the time of search asymptotically does not depend on the size of the collection.

The collection data structure classify data into buckets (please see the links above) having the same value of hash code, which is the key of the search algorithm.

See also:
http://www.sgi.com/tech/stl/HashedAssociativeContainer.html[^],
http://en.wikipedia.org/wiki/Hash_table[^],
http://en.wikipedia.org/wiki/Big_O_notation[^].

Whenever you define a type and override equality method System.Object.Equals, you also need to override System.Object.GetHashCode. Please see:
http://msdn.microsoft.com/en-us/library/system.object.aspx[^].

This is quite natural: you cannot consistently implement one without another preserving correct behavior of the type as a collection key.



Logically equal objects should manifest equal hash code values, and unequal objects should return values of the hash code which are different with high probability — this is the major requirement for the class implementation.

You might be puzzled to know how should you implement hash code for your classes when it is required. I''ll give you a very simple recipe. Consider your class has several fields which are use in equals implementation. Supposing they already have implementation of hash code, take all their hash code values and make a binary XOR of all of them. Return the resulting value as a hash code of the composing object. That''s it.

For a home exercise, think why this implementation works. :-)

—SA


查看这篇文章



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



不,它与垃圾收集无关。
check this article

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

No, it has nothing to do with garbage collection.


MSDN 有关于您的问题的所有答案,探索链接

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

http://msdn.microsoft.com/en-us/lib rary / system.object.gethashcode(v = vs.71).aspx [ ^ ]
MSDN has all the answers regarding your question, explore links :
http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx[^]
http://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.71).aspx[^]


这篇关于C#.Net中的Object.GetHashCode()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆