#407 – Why You Should Override GetHashCode when Overriding Equals
September 8, 2011 1 Comment
Whenever you override the Equals method for a class, you should also override the GetHashCode method.
GetHashCode is a method that provides values to use as keys in a hash table. This value, the hash, is just a number that represents the value of an object, without having to look at the entire object. The hash code can also be thought of as a key used to look the object up in a hash table.
Objects that are equivalent should always return the same hash code.
If you don’t implement GetHashCode for your object and the object ends up stored in a Dictionary or HashTable, it’s possible that the algorithms for sorting or finding elements in these structures will not work properly.