What are the disadvantages of using Guid (). GetHashCode () when overriding GetHashCode ()

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

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

I even thought that Equals look right, is it right to say that this implementation will cause many assumptions that .NET will 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 hear is that GetHashCode should not change after the object is created. This is due to the fact that this object is stored in a dictionary.

Can someone clarify this for me and explain what should happen with GetHashCode if the object changes, which ultimately changes the Equals method?

+4
source share
2 answers

From the MSDN section (Artist Notes) :

The hash function must have the following properties:

  • If two objects are compared as equal, the GetHashCode method for each object must return the same value. However, if two objects are not compared as equal, the GetHashCode methods for the two objects should not return different values.

  • The GetHashCode method for an object must consistently return the same hash code if there is no change in the state of the object that determines the return value of the Equals method of the object. Note that this is true only for the current execution of the application, and that a different hash code may be returned if the application starts again.

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

Depending on the Equals method for this object, you may also break the first point from the documentation.

Better reading

+2
source

A Hash is a one-way math function that takes input and provides reproducible output.

Hash is often used to identify data that is not identifiable by itself, so if you compute a hash on a data block, that data should always create the same hash. one example is passwords. when you register on the site, they save the hash of your password using an algorithm. when you log in, you send your password to a site that hashes it using the same algorithm that they used to store it. if the two hash values โ€‹โ€‹match, then you entered the correct password.

if the object modifies the computed hash will be different. this is often important for checking data. if I use sha1 to hash the string "Frank" at 123456789 (just an example), and I send the data to them along with the hash, they can perform the same hash and see if the values โ€‹โ€‹match. if my bits are mixed up when sending and they get a โ€œBrankโ€ when they calculate the hash, it will not be 123456789 and they know that the data was corrupted when sending.

Using NewGuid , you simply generate a random number that is not related to the original data. it cannot be reproduced, so all the above examples would not be possible. the hash algorithm should always provide the same output for the same input, and it should also try to prevent any other input from being created for the same output.

Hope that helps

+1
source

Source: https://habr.com/ru/post/1440407/


All Articles