471,326 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 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 1286
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Franco, Gustavo | last post: by
7 posts views Thread by Avin Patel | last post: by
21 posts views Thread by Helge Jensen | last post: by
5 posts views Thread by Stoyan | last post: by
8 posts views Thread by Matthias Kientz | last post: by
2 posts views Thread by perspolis | last post: by
5 posts views Thread by Metaman | last post: by
28 posts views Thread by Tony Johansson | last post: by
reply views Thread by rosydwin | last post: by

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.