Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 IndyOfComo   Members   -  Reputation: 584

Like
0Likes
Like

Posted 17 September 2013 - 02:10 PM

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.


Here is my technical background info.

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9597

Like
2Likes
Like

Posted 17 September 2013 - 02:31 PM

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



#3 dilyan_rusev   Members   -  Reputation: 950

Like
0Likes
Like

Posted 18 September 2013 - 10:11 AM

... and if you have to roll your own, I've always found the general algorithm proposed in Advanced Java. It is illustrated in this article about Java's object.hashCode().



#4 cdoubleplusgood   Members   -  Reputation: 839

Like
0Likes
Like

Posted 18 September 2013 - 10:28 AM


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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS