473,378 Members | 1,140 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,378 software developers and data experts.

Appropriate GetHashCode() override?

Hi,
I have a moderately complex class (about 10 different string and int
type fields along with two different List<stringfields). I want to override
Equals(), so I need to override GetHashCode(). The standard override for
GetHashCode() for something would be to join GetHashCode for each field with
^ (XOR). Right? But, in this case, one of the fields is a "Unique" Identifier
and SHOULD uniquely identify the instance of the class. If that is the case,
is it acceptable to just use the HashCode for that uniqueID field as the
HashCode for the instance? What if there are two different instances with
the same "Unique" identifier, but different contents (which is what I am
looking for with my Equals() override)? If they evaluate to the same HashCode
will that cause major issues?

Thanks!
Ethan
Nov 14 '08 #1
6 8215
Lee
Ethan,

There is a great blog entry about this very thing here:
http://blog.roblevine.co.uk/?p=13

But to summarize, if two things are Equal(), there HashCode MUST be
the same and if they are not equal, they CANNOT be the same.

Then again, all this is moot if you never are going to use the class
is a container that uses the HashCode. (Not usually a safe bet due to
possible future enhancements to the codebase)

L. Lee Saunders
http://oldschooldotnet.blogspot.com
Nov 14 '08 #2
On Fri, 14 Nov 2008 09:25:00 -0800, Lee <sa******@hotmail.comwrote:
Ethan,

There is a great blog entry about this very thing here:
http://blog.roblevine.co.uk/?p=13

But to summarize, if two things are Equal(), there HashCode MUST be
the same and if they are not equal, they CANNOT be the same.
That's not a reasonable summary of the article, or of the requirements of
a hash code. It's true that if two things are equal, the hash codes
returned by those things must also be equal. But it is definitely _not_
true that when the two things are not equal that the hash codes "CANNOT be
the same". In fact, if that were possible then we could skip using the
Equals() method altogether and only use GetHashCode().
Then again, all this is moot if you never are going to use the class
is a container that uses the HashCode. (Not usually a safe bet due to
possible future enhancements to the codebase)
If you override Equals(), you should definitely override GetHashCode() to
match.

Pete
Nov 14 '08 #3
On Fri, 14 Nov 2008 08:44:13 -0800, Ethan Strauss
<Et**********@discussions.microsoft.comwrote:
Hi,
I have a moderately complex class (about 10 different string and int
type fields along with two different List<stringfields). I want to
override
Equals(), so I need to override GetHashCode(). The standard override for
GetHashCode() for something would be to join GetHashCode for each field
with
^ (XOR). Right? But, in this case, one of the fields is a "Unique"
Identifier
and SHOULD uniquely identify the instance of the class.
"Uniquely identify" how? If that field isn't the sole field used for the
Equals() method, then why not? Why bother to check other fields when this
one field can tell you that two instances are equal or not?

Conversely, if more than just this one field is involved in equality, then
how can you say that a single field does in fact "uniquely identify the
instance of the class"? I mean, literally speaking the _reference_ to the
instance is a unique identifier, but if that were really sufficient for
your needs, you wouldn't have to override Equals() at all. So you must
not mean the _instance_ is uniquely identified but rather the data
represented by it.
If that is the case,
is it acceptable to just use the HashCode for that uniqueID field as the
HashCode for the instance? What if there are two different instances
with
the same "Unique" identifier, but different contents (which is what I am
looking for with my Equals() override)? If they evaluate to the same
HashCode
will that cause major issues?
You need to step back and figure out why your definitions of "unique" and
"equals" don't match. It is true that you must _not_ use that single
field for your hash code if other fields are included in the Equals()
test, but the very fact that you're asking about this means that you
haven't fully resolved what it means for two instances of your class to be
equal. Until you figure that out, you can't possible successfully
implement Equals() and GetHashCode().

By the way, do _not_ composite hash codes using XOR. You will
unnecessarily introduce collisions at a remarkably high rate. The
accepted practice is to use an accumulator, multiplying by a prime number
between each addition of the next field's hash code. Search this
newsgroup for past discussions of hash codes for more details.

Pete
Nov 14 '08 #4
Hi Pete,
Thanks for you comments. In this case, I am populating this class by
pulling data from a database using a WebService. The "Unique" identifier
uniquely identifies a record in the main database table, but I am getting a
bunch of related data as well and I suspicious that I not always getting
exactly the same related data. One of my main goals right now is to see if it
is, in fact, always the same. I guess this means that I don't consider it
unique and should write GetHashCode as though it is not. I will probably go
ahead and rewrite the GetHashCode as described in
http://blog.roblevine.co.uk/?p=19.
>By the way, do _not_ composite hash codes using XOR. You will
unnecessarily introduce collisions at a remarkably high rate. The
accepted practice is to use an accumulator, multiplying by a prime number
between each addition of the next field's hash code. Search this
newsgroup for past discussions of hash codes for more details.
Given this (and I now agree with it), Microsoft should really change the
documentation for GetHashCode
(http://msdn.microsoft.com/en-us/libr...thashcode.aspx ).
This more or less tells you to just XOR everything together.

Ethan

Nov 14 '08 #5
Ethan Strauss wrote:
I have a moderately complex class (about 10 different string and int
type fields along with two different List<stringfields). I want to override
Equals(), so I need to override GetHashCode(). The standard override for
GetHashCode() for something would be to join GetHashCode for each field with
^ (XOR). Right? But, in this case, one of the fields is a "Unique" Identifier
and SHOULD uniquely identify the instance of the class. If that is the case,
is it acceptable to just use the HashCode for that uniqueID field as the
HashCode for the instance?
If the Equals overload just compares those ids, then having HashCode
return the has code of the id is a good solution.
What if there are two different instances with
the same "Unique" identifier, but different contents (which is what I am
looking for with my Equals() override)? If they evaluate to the same HashCode
will that cause major issues?
Having Equals compare other fields than the id and have HashCode return
only the hash code of the id is valid, but in my opinion not good style.

Arne

Nov 15 '08 #6
Lee wrote:
There is a great blog entry about this very thing here:
http://blog.roblevine.co.uk/?p=13

But to summarize, if two things are Equal(), there HashCode MUST be
the same and if they are not equal, they CANNOT be the same.
I hope that your summary are wrong, since in general objects
can return same HashCode even if Equals return false.

Arne
Nov 15 '08 #7

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

Similar topics

4
by: Bill Mittenzwey | last post by:
Ok, I've read in the docs, some books and some articles by prominant dotneters about how to override GetHashCode, but it still leaves me somewhat puzzled. I have a custom object which I want to...
4
by: Guinness Mann | last post by:
resource.cs(8,15): warning CS0659: 'UA.LMS.Resource.Resource' overrides Object.Equals(object o) but does not override Object.GetHashCode() What does this mean? I mean I know what it says, but do...
2
by: Fuzzy | last post by:
I have defined a struct in my application that contains 3 integers that maintain state information. I have a lot of these structs in time-critical portions of my app, so they must be as fast as...
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...
3
by: cmrchs | last post by:
Hi, why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ? thnx Chris ...
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...
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) {...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
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.