473,379 Members | 1,533 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,379 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 1368
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.