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==ca rd2.rank);
}
public override bool Equals(object card)
{
return this == (Card) card;
}
public override int HetHashCode()
{
return 13*(int)rank + (int)suit;
}
....
....
}
//Tony 10 1404
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
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*********@nn owslpianmk.coms krev 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
On Jun 13, 8:39 am, "Tony" <johansson.ande rs...@telia.com wrote:
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
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
At least override it and throw a NotImplementedE xception.
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*********@nn owslpianmk.coms krev 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
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*********@nn owslpianmk.coms krev 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
On Jun 13, 9:51 am, "Tony" <johansson.ande rs...@telia.com wrote:
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
On Jun 13, 10:03 am, "Tony" <johansson.ande rs...@telia.com wrote:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Application.ExitThread in the same thread that it is running.
|
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 override int GetHashCode()
int hash = 0
for (int i = 0; i < data.Length; i++
|
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 by Sets.
- ICollection cannot decide containment
- IList promises indexability by the natural numbers, which is not
achievable (since i hash elements, not sort them).
- IDictionary is definatly not setlike.
Although I can, of course, define...
|
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;
otherwise, Hashtable might not work correctly."
Does any one know, why we must also override Equals,
Please give my an example:)
Thanks,
Stoyan
|
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...
| |
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
|
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)
{
try
{
Type objectType = input.GetType();
PropertyInfo properties = objectType.GetProperties();
|
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
{
....
}
and I am trying to implement the Equals and GetHashCode methods.
|
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 usefulness of this
GetHashCode or it it of no use at all?
A mean that if I get an integer from current time in some way what should I
use it for?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |