Jump to content

View more

Image of the Day

The night is still, but the invasion brings chaos. #screenshotsaturday #hanako #indiegame #gameart #ue4 #samurai https://t.co/cgILXuokoS
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

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

4: Adsense

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   

835
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.

#2 SiCrane   Moderators   

11764
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   

1360
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   

895
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.