How does the default implementation for GetHashCode()
work? And does it handle structures, classes, arrays, etc. efficiently and well enough?
I am trying to decide in what cases I should pack my own and in what cases I can safely rely on the default implementation to do well. I don't want to reinvent the wheel, if at all possible.
For a class, the defaults are essentially reference equality, and that is usually fine. If writing a struct, it is more common to override equality (not least to avoid boxing), but it is very rare you write a struct anyway!
When overriding equality, you should always have a matching Equals()
and GetHashCode()
(i.e. for two values, if Equals()
returns true they must return the same hash-code, but the converse is not required) - and it is common to also provide ==
/!=
operators, and often to implement IEquatable<T>
too.
For generating the hash code, it is common to use a factored sum, as this avoids collisions on paired values - for example, for a basic 2 field hash:
unchecked // disable overflow, for the unlikely possibility that you
{ // are compiling with overflow-checking enabled
int hash = 27;
hash = (13 * hash) + field1.GetHashCode();
hash = (13 * hash) + field2.GetHashCode();
return hash;
}
This has the advantage that:
etc - which can be common if just using an unweighted sum, or xor (^
), etc.