.NET:是Type.GetHash code保证是唯一的?是唯一、Type、NET、code

由网友(一刀斩到你桃花开)分享简介:我在使用Type.GetHash code的人,就好像它是一个主键。我认为这是一个可怕的想法,但我想知道是否有某种记录的特殊情况,说没有两个类型将具有相同的哈希code。I have someone using Type.GetHashCode as if it were a primary key. I think...

我在使用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,如果能在应用程序运行被退回   再次

eclipse中不能自动添加Source 的get和set方法还有hashCode 之类的方法,是我缺少东西嘛

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.

您应该看密码散列的,如果你想要的几乎的基于对象的值唯一的哈希code。

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

阅读全文

相关推荐

最新文章