what happens if I skip implementing GetHashCode

T

Tony

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
 
T

Tony

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 said:
[...]
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
 
J

Jon Skeet [C# MVP]

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
 
T

Tony

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
 
T

Tony

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
 
J

Jon Skeet [C# MVP]

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
 
J

Jon Skeet [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top