473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

what happens if I skip implementing GetHashCode

Hello!

Here I have a Card class with several methods.

Now to my question I don't really fully understand what purpose this
GetHashCode is used for.
That's why I bring it up again.
In this simple example below with the Card class what would happen if I skip
this GetHashCode in this Card class?
Is it possible to write some simple test program to prove that this
GetHashCode will prevent
error or problem to occur?

class Card
{
//rank and suit are defined here
public static bool operator==(Card card1, Card card2)
{
return card1.suit == card2.suit) && (card1.rank==card2.rank);
}

public override bool Equals(object card)
{
return this == (Card) card;
}

public override int HetHashCode()
{
return 13*(int)rank + (int)suit;
}
....
....
}

//Tony
Jun 27 '08 #1
10 1371
On Fri, 13 Jun 2008 00:01:31 -0700, Tony <jo*****************@telia.com>
wrote:
[...]
Now to my question I don't really fully understand what purpose this
GetHashCode is used for.
That's why I bring it up again.
In this simple example below with the Card class what would happen if I
skip
this GetHashCode in this Card class?
Then hash table implementations will treat two instances that might have
compared as equal as if they are not equal. Likewise anything else that
might use the hash code.

If you never use instances of this class as keys in a hash table or with
anything else that cares about the hash code, it won't matter.

If you do decide to go ahead and implement GetHashCode() (and IMHO, why
not?), you should probably spell the name of the method correctly. :)

If you need more information about how hash codes are used, I'd suggest a
good computer science text on the subject. A newsgroup isn't the best way
to educate oneself on such things, it being a relatively advanced topic
deserving of much more than one or a few newsgroup posts.

Pete
Jun 27 '08 #2
Hello!!

Now we assume that I store instances of Card as key in some sort of hash
table or similar things.

According to you will the hash table implementations treat two instances
that might have
compared as equal as if they are not equal.what will happen then?

//Tony

"Peter Duniho" <Np*********@nnowslpianmk.comskrev i meddelandet
news:op***************@petes-computer.local...
On Fri, 13 Jun 2008 00:01:31 -0700, Tony <jo*****************@telia.com>
wrote:
[...]
Now to my question I don't really fully understand what purpose this
GetHashCode is used for.
That's why I bring it up again.
In this simple example below with the Card class what would happen if I
skip
this GetHashCode in this Card class?

Then hash table implementations will treat two instances that might have
compared as equal as if they are not equal. Likewise anything else that
might use the hash code.

If you never use instances of this class as keys in a hash table or with
anything else that cares about the hash code, it won't matter.

If you do decide to go ahead and implement GetHashCode() (and IMHO, why
not?), you should probably spell the name of the method correctly. :)

If you need more information about how hash codes are used, I'd suggest a
good computer science text on the subject. A newsgroup isn't the best way
to educate oneself on such things, it being a relatively advanced topic
deserving of much more than one or a few newsgroup posts.

Pete

Jun 27 '08 #3
On Jun 13, 8:39 am, "Tony" <johansson.anders...@telia.comwrote:
Now we assume that I store instances of Card as key in some sort of hash
table or similar things.

According to you will the hash table implementations treat two instances
that might have
compared as equal as if they are not equal.what will happen then?
Suppose you store some value with the key "10 of spades". Later you
create a new but equal "10 of spades" - you'd like to be able to look
up the value in the hash table using the new "10 of spades", but if it
gives a different hash code to the original key, the hashtable won't
find the entry.

Jon
Jun 27 '08 #4
On Fri, 13 Jun 2008 00:39:06 -0700, Tony <jo*****************@telia.com>
wrote:
Hello!!

Now we assume that I store instances of Card as key in some sort of hash
table or similar things.

According to you will the hash table implementations treat two instances
that might have
compared as equal as if they are not equal.what will happen then?
It will be as if the two aren't equal, just as I said. In that scenario,
you might as well not have overridden the Equals() method, because the
test for equality in the hash table will never get that far. The hash
table won't even bother calling Equals() unless GetHashCode() returns the
same value, and if you don't override GetHashCode() it won't except for
the same instance.

And of course, the consequence of that will be the same as for any other
reason that the key might test as not equal. You won't find an entry in
the hash table except by using the exact instance used to add it to the
hash table, and adding a value to the hash table using a key other than
the exact instance used to add some other value previously will succeed,
even if the key's Equals() methods would have returned true for some other
key already in the hash table.

Pete
Jun 27 '08 #5
At least override it and throw a NotImplementedException.
Jun 27 '08 #6
Hello!

Well it's not possible to add two key that are the same to a hashtable.
Have I missed something here?
Jon mentioned that the key was "10 of spades" when trying to add this a
second time it will complain?

//Tony

"Peter Duniho" <Np*********@nnowslpianmk.comskrev i meddelandet
news:op***************@petes-computer.local...
On Fri, 13 Jun 2008 00:39:06 -0700, Tony <jo*****************@telia.com>
wrote:
Hello!!

Now we assume that I store instances of Card as key in some sort of hash
table or similar things.

According to you will the hash table implementations treat two instances
that might have
compared as equal as if they are not equal.what will happen then?

It will be as if the two aren't equal, just as I said. In that scenario,
you might as well not have overridden the Equals() method, because the
test for equality in the hash table will never get that far. The hash
table won't even bother calling Equals() unless GetHashCode() returns the
same value, and if you don't override GetHashCode() it won't except for
the same instance.

And of course, the consequence of that will be the same as for any other
reason that the key might test as not equal. You won't find an entry in
the hash table except by using the exact instance used to add it to the
hash table, and adding a value to the hash table using a key other than
the exact instance used to add some other value previously will succeed,
even if the key's Equals() methods would have returned true for some other
key already in the hash table.

Pete

Jun 27 '08 #7
Hello!

If I for example use a string as a key and a person as a value and add this
item to a hashtable this will never be any
problem because it matters only when key is a instance.
Is this right?

//Tony

"Peter Duniho" <Np*********@nnowslpianmk.comskrev i meddelandet
news:op***************@petes-computer.local...
On Fri, 13 Jun 2008 00:39:06 -0700, Tony <jo*****************@telia.com>
wrote:
Hello!!

Now we assume that I store instances of Card as key in some sort of hash
table or similar things.

According to you will the hash table implementations treat two instances
that might have
compared as equal as if they are not equal.what will happen then?

It will be as if the two aren't equal, just as I said. In that scenario,
you might as well not have overridden the Equals() method, because the
test for equality in the hash table will never get that far. The hash
table won't even bother calling Equals() unless GetHashCode() returns the
same value, and if you don't override GetHashCode() it won't except for
the same instance.

And of course, the consequence of that will be the same as for any other
reason that the key might test as not equal. You won't find an entry in
the hash table except by using the exact instance used to add it to the
hash table, and adding a value to the hash table using a key other than
the exact instance used to add some other value previously will succeed,
even if the key's Equals() methods would have returned true for some other
key already in the hash table.

Pete

Jun 27 '08 #8
On Jun 13, 9:51 am, "Tony" <johansson.anders...@telia.comwrote:
Well it's not possible to add two key that are the same to a hashtable.
Have I missed something here?
Jon mentioned that the key was "10 of spades" when trying to add this a
second time it will complain?
I wasn't suggesting adding it a second time - just using it to fetch.

However, if you *did* try to add it a second time, you would succeed
if the hash code was different, because again the hashtable wouldn't
be able to detect that the key was already present.

Jon
Jun 27 '08 #9
On Jun 13, 10:03 am, "Tony" <johansson.anders...@telia.comwrote:
If I for example use a string as a key and a person as a value and add this
item to a hashtable this will never be any
problem because it matters only when key is a instance.
Is this right?
Exactly. Values aren't checked for equality and their hashcodes aren't
used.

Jon
Jun 27 '08 #10
On Fri, 13 Jun 2008 01:51:10 -0700, Tony <jo*****************@telia.com>
wrote:
Well it's not possible to add two key that are the same to a hashtable.
Have I missed something here?
Yes. You keep thinking that you are talking about "two key that are the
same". But that's the point: if the hash code is different, the two keys
aren't the same. Even if the Equals() method says they are.

Pete
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Franco, Gustavo | last post by:
Hi, I have a question, and please I need a answer. How can I finalize a thread running with Application.Run (I need the message loop!!!) without call Thread.Abort?. I want to call...
7
by: Avin Patel | last post by:
Hi I have question for GetHashCode() function, Is it correct in following code or there is more efficient way to implement GetHashCode() function class IntArray public int data public...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
8
by: Matthias Kientz | last post by:
I have a class which should override the GetHashcode() function, which look like this: public class A { public string str1; public string str2; //other methods...
2
by: perspolis | last post by:
Hi I have a question about GetHashCode for strings. Is it unique for all string in diffrent OS? for example "test".GetHashCode () is the same for all of OS? thanks in advance
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
6
by: Raj Wall | last post by:
Hi, I am trying to implement the IEqualityComparer interface for a struct so I can use it as the Key for a Dictionary. My struct declaration has: public struct Ring : IEqualityComparer {...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.