.NET:是Type.GetHash code保证是唯一的? [英] .NET: Is Type.GetHashCode guaranteed to be unique?

查看:176
本文介绍了.NET:是Type.GetHash code保证是唯一的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Type.GetHash code的人,就好像它是一个主键。我认为这是一个可怕的想法,但我想知道是否有某种记录的特殊情况,说没有两个类型将具有相同的哈希code。

I have someone using Type.GetHashCode as if it were a primary key. I think this is a horrible idea but I wanted to know if there was some sort of documented special case that says no two types would have the same hash code.

推荐答案

大约有GetHash code无保证,除了它很可能会的随机分布的,不是唯一的。 文档特别提到:

There are no guarantees around GetHashCode except that it will likely be randomly distributed, not unique. Documentation specifically mentions that:

在GetHash code方法的默认实现不   针对不同的对象保证唯一的返回值。此外,   .NET框架并不能保证的默认实现   在GetHash code方法,它会返回将是相同的值   不同版本的.NET Framework之间。因此,   此方法的缺省的实现不能用作唯一   散列目的对象标识符。 ......如果两个对象不比较平等,这两个对象的的GetHash code方法不必返回不同的值

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes. ... if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

随机分布鼓励以避免哈希冲突(慢词典):

Random distribution is encouraged to avoid hash collisions (slow Dictionaries):

为了获得最佳性能,哈希函数必须生成一个随机的   分布于所有的输入。

For the best performance, a hash function must generate a random distribution for all input.

这也是一个非常糟糕的主意坚持的GetHash code结果和基础的任何决定,这个持续的价值。同样的对象可能会返回不同的散列code对下一个应用程序执行:

It is also a very bad idea to persist results of GetHashCode and base any decisions on this persisted value. The same object may return different hash code on a next application execution:

为对象的GetHash code方法必须一致地返回相同   哈希code,只要没有修改对象的状态   确定对象的Equals方法的返回值。注意   这是真实的,只有一个应用程序的当前执行,并   一个不同的hash code,如果能在应用程序运行被退回   再次

The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.

CLR本身改变 GetHash code实现.NET 1和.NET 2之间的字符串使用不同的哈希算法为32位和64位版本。

CLR itself changed GetHashCode implementation for a String between .NET 1 and .NET 2 and uses different hash algorithm for 32 and 64 bit versions.

准则和GetHash $规则C $Ç

GetHash code是专门做的只有一件事:平衡哈希表。做   不会用它做别的事情。

GetHashCode is designed to do only one thing: balance a hash table. Do not use it for anything else.

您应该看<一href="http://blogs.msdn.com/b/securitytools/archive/2009/08/27/hash-functions-in-net-right-tool-for-the-right-job.aspx"相对=nofollow>密码散列的,如果你想要的几乎的基于对象的值唯一的哈希code。

You should be looking at cryptographic hashes if you want almost unique hashcode based on the object value.

这篇关于.NET:是Type.GetHash code保证是唯一的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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