• Advertisement
Sign in to follow this  

Overwriting Equal() and get-hash funct. for HashTable

This topic is 1586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So the MSDN for HashTables says:

The objects used as keys by a Hashtable are required to override the Object.GetHashCode method (or the IHashCodeProviderinterface) and the Object.Equals method (or the IComparer interface).

 

 

However, is that really necessary for elemental data types? The example further down that page doesn't show any kind of overwriting for the string key.

Share this post


Link to post
Share on other sites
Advertisement

Well it is necessary, but it's already done for you. All the basic types already have perfectly usable implementations of GetHashCode().

Share this post


Link to post
Share on other sites


However, is that really necessary for elemental data types? The example further down that page doesn't show any kind of overwriting for the string key.

When you write your own value types (struct), and use them as keys, you should (really!) implement Equals and GetHashCode because the default implementation is more or less useless.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement