.NET Generic Dictionary<K,V> - Using custom types as the Key
<font class="messagecontent">If I create a Generic Dictionary<K,V> collection object and use my own custom class as the Key, do I have to override the Equals(object o) and GetHashCode() methods for my custom class?
know I have to do this if I use the non-generic version of Dictionary,
the HashTable class, according to MSDN documentation anyway, but I
can't find anything about how this rule applies to the Generic
Dictionary<K,V> collection type. I'm assuming not, but then how
does the Dictionary hash the key internally?
Lets think about this for a moment.
A HashTable calls GetHashCode and Equals to determine the equality of two objects. Since its not typed, it doesn't really care about types.
A Generic HashTable knows about types. How could it then determine hashcode and equalality? IT CAN'T
In other words: Yes, you still have to follow the same contract regarding equals/hash.