简体   繁体   English

重写GetHashCode()时使用Guid()。GetHashCode()有什么缺点

[英]What are the drawbacks of using a Guid().GetHashCode() when overriding GetHashCode()

I found an implementation of GetHashCode() that looks like this 我发现GetHashCode()的实现看起来像这样

    Guid _hashCode = Guid.NewGuid();
    public override int GetHashCode()
    {
        return _hashCode.GetHashCode();
    }

Even thought the Equals looks correct, is it correct to say this implementation will cause many assumptions about .NET to break? 即使认为Equals看起来是正确的,说这个实现会导致许多关于.NET的假设破裂是否正确?

       public override bool Equals(object obj)
    {
        if (obj.GetType() != trustedEntity.GetType())
            return false;

        TrustedEntity typedObj = (TrustedEntity)obj;

        if (trustedEntity.BackTrustLink != typedObj.BackTrustLink)
            return false;
        if (trustedEntity.ForwardTrustLink != typedObj.ForwardTrustLink)
            return false;
        if (trustedEntity.EntryName != typedObj.EntryName)
            return false;

        return true;
    }

The counter argument I'm hearing is that GetHashCode needs to never change once the object is created. 我听到的反驳论点是,一旦创建了对象,GetHashCode就永远不会改变。 This is because this object is stored in a dictionary. 这是因为该对象存储在字典中。

Can someone clear this up for me and explain what needs to happen to GetHashCode if the object changes, that ultimately changes the Equals method? 有人可以为我清除这一点并解释如果对象发生变化,GetHashCode需要发生什么,最终会改变Equals方法吗?

From MSDN (Notes to Implementers section) : 从MSDN(Notes to Implementers部分)

A hash function must have the following properties: 哈希函数必须具有以下属性:

  1. If two objects compare as equal, the GetHashCode method for each object must return the same value. 如果两个对象比较相等,则每个对象的GetHashCode方法必须返回相同的值。 However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values. 但是,如果两个对象的比较不相等,则两个对象的GetHashCode方法不必返回不同的值。

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

  3. For the best performance, a hash function must generate a random distribution for all input. 为获得最佳性能,哈希函数必须为所有输入生成随机分布。

Depending on the Equals method for this object, you may be violating the first point from the documentation as well. 根据此对象的Equals方法,您可能也违反了文档中的第一点。

More excellent reading 更优秀的阅读

A Hash is a one way mathematical function that takes an input and provides a reproducible output. 哈希是一种单向数学函数,它接受输入并提供可重现的输出。

Hash's are often used to identify data that is itself non-identifying, so if you calculate a hash on a block of data, that data should always create the same hash. 散列通常用于标识本身不可识别的数据,因此如果计算数据块的散列,则该数据应始终创建相同的散列。 one example is passwords. 一个例子是密码。 when you register for a site, they store a hash of your password via an algorithm. 当您注册网站时,他们会通过算法存储您的密码哈希值。 when you login you send your password to the site, which hashes it using the same algorithm they used when storing it. 当您登录时,您将密码发送到网站,使用他们在存储时使用的相同算法对其进行哈希处理。 if the two hash values match, then you have entered your correct password. 如果两个哈希值匹配,则表示您输入了正确的密码。

if the object changes the hash computed would be different. 如果对象发生更改,则计算的哈希值会有所不同。 this is often important for data verification. 这对于数据验证通常很重要。 if I use sha1 to hash the string 'Frank' into 123456789 (just an example), and I send the data to them, along with the hash, they can perform the same hashing and see if the values match. 如果我使用sha1将字符串'Frank'哈希到123456789(仅作为示例),并且我将数据与哈希一起发送给它们,它们可以执行相同的哈希并查看值是否匹配。 if my bits get messed up in sending, and they receive 'Brank', when they compute the hash, it will not be 123456789, and they know the data was corrupted in the sending. 如果我的位在发送时搞砸了,并且他们收到'Brank',当他们计算哈希时,它不会是123456789,并且他们知道数据在发送中被破坏了。

By using NewGuid , you are simply generating a random number that has not relationship to the original data. 通过使用NewGuid ,您只需生成与原始数据无关的随机数。 it cannot be reproduced, so all the examples above would not be possible. 它不能被复制,所以上面的所有例子都是不可能的。 a hash algorithm must always provide the same output for the same input, and it must also attempt to prevent any other input from generating that same output. 哈希算法必须始终为同一输入提供相同的输出,并且还必须尝试阻止任何其他输入生成相同的输出。

Hope that helps 希望有所帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM