Pregunta

I found an implementation of GetHashCode() that looks like this

    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?

       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. 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?

¿Fue útil?

Solución

From MSDN (Notes to Implementers section):

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. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

  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. 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.

More excellent reading

Otros consejos

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. 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.

By using NewGuid, you are simply generating a random number that has not relationship to the original data. 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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top