.NET:Type.GetHashCode 是否保证唯一?

发布于 2024-12-05 09:37:15 字数 87 浏览 0 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病毒体 2024-12-12 09:37:15

GetHashCode 没有任何保证,除了它可能是随机分布,而不是唯一的。 文档特别提到:

GetHashCode 方法的默认实现
保证不同对象的唯一返回值
。此外,
.NET Framework 不保证默认实现
GetHashCode方法,它返回的值将是相同的
.NET Framework 的不同版本之间。因此,
此方法的默认实现不得用作唯一的
用于散列目的的对象标识符。 ...如果两个对象比较不相等,则两个对象的 GetHashCode 方法不必返回不同的值

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

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

保存 GetHashCode 的结果并基于此保存的值做出任何决策也是一个非常糟糕的主意。同一对象可能会在下一次应用程序执行时返回不同的哈希代码:

对象的 GetHashCode 方法必须始终返回相同的值
哈希码只要没有修改对象状态即可
确定对象的 Equals 方法的返回值。注意
这仅适用于应用程序的当前执行,并且
如果应用程序运行,可以返回不同的哈希代码
再次。

CLR 本身更改了之间的字符串的 GetHashCode 实现.NET 1 和 .NET 2 并针对 32 位和 64 位版本使用不同的哈希算法。

来自 GetHashCode 指南和规则

GetHashCode 的设计目的只是做一件事:平衡哈希表。做
不要将其用于其他任何用途。

您应该查看 加密哈希(如果您想要基于对象值的几乎唯一的哈希代码)。

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

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.

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:

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 itself changed GetHashCode implementation for a String between .NET 1 and .NET 2 and uses different hash algorithm for 32 and 64 bit versions.

From Guidelines and rules for GetHashCode:

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

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

冷默言语 2024-12-12 09:37:15

保证是唯一的。

如果您的程序集是强命名的,您可以使用完全限定的类型名称作为唯一键来标识类型

It's not guaranteed to be unique.

If your assemblies are strongly named you could use the fully qualified type name as a unique key to identify a Type.

乱了心跳 2024-12-12 09:37:15

为对象生成哈希码的目标是在给定数据类型的情况下尽可能唯一,以避免表中发生冲突。但是,这绝对不能保证。许多哈希表实现将每个哈希代码桶链接起来(数组列表)以处理冲突。

The goal of producing a hash code for an object is to be as unique as possible given the type of data to avoid collisions in the table. But, it's absolutely not guaranteed. Many hash table implementations chain (an array list) off of each hash code bucket to deal with collisions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文