Possible Duplicate:
Why is it important to override GetHashCode when Equals method is overridden?
In C#, what specifically can go wrong if one fails to override GetHashCode() when overriding Equals()?
The most visible way is for mapping structures.
Any class which does this will have unpredictable behavior when used as the Key for a Dictionary or HashTable. The reason being is that the implementation uses both GetHashCode and Equals to properly find a value in the table. The short version of the algorithm is the following
Failing to keep GetHashCode and Equals in sync will completely break this algorithm (and numerous others).
Think of a hash / dictionary structure as a collection of numbered buckets. If you always put things in the bucket corresponding to their GetHashCode(), then you only have to search one bucket (using Equals()) to see if something is there. This works, provided you're looking in the right bucket.
So the rule is: if Equals() says two objects are Equal(), they must have the same GetHashCode().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With