473,378 Members | 1,401 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.

GetHashCode override

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 be able to have sorted correctly in a
sortedlist/hashtable... For that to work correctly, I need to override
Equals(). Fair enough, otherwise the sort would not really know what
represents the value of my object. Then the compiler tells me that I need to
also override the GetHashCode. At first thought, it seems easy, just pass
the call on to my describing property ( a stirng) and it would seem to work.

Ah, but that seems to break the rules for GetHashCode. As I see it, I need
to guarantee that all objects which represent the same value return the same
Hash Code (fine so far), and I need to guarantee that once an object has
supplied a hash code, it will never change. Conflicts start here. What if I
have two objects which have there properties set to two different values,
and so have reported back two different hash values. Then a program changes
the property of one of the objects to be the same as the other. Now I have
two objects which represent the same value, but with two different hash
codes. I seem to be stuck in a catch-22 here. I have to have all objects
representing the same value return the same hash, but I can't change the
hash to make a changed object compliant. What if the objects I wanted to
sort were based off of a property like priority. It is reasonable to have
multiple objects with the same priority in a list, and I would want the
objects to be sorted in the list based off the priority. Also it is common
for a priority to change, in which case, I would want that object to move to
a different location in the list.

Can anyone explain this? Am I just misusing the lists?
Jul 21 '05 #1
4 6613
Bill Mittenzwey <bm*********@hotmail.com> wrote:
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 be able to have sorted correctly in a
sortedlist/hashtable... For that to work correctly, I need to override
Equals(). Fair enough, otherwise the sort would not really know what
represents the value of my object. Then the compiler tells me that I need to
also override the GetHashCode. At first thought, it seems easy, just pass
the call on to my describing property ( a stirng) and it would seem to work.
Yup.
Ah, but that seems to break the rules for GetHashCode. As I see it, I need
to guarantee that all objects which represent the same value return the same
Hash Code (fine so far), and I need to guarantee that once an object has
supplied a hash code, it will never change. Conflicts start here. What if I
have two objects which have there properties set to two different values,
and so have reported back two different hash values. Then a program changes
the property of one of the objects to be the same as the other. Now I have
two objects which represent the same value, but with two different hash
codes.


As soon as types are mutable, you've got a problem whenever they're
stored in hash tables, sorted lists or whatever. Don't forget that
Equals will suddenly change as well - something that *was* Equal to
another thing possibly won't be any more.

Do you *definitely* need your property to be settable?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
After some thought, I would consider making that particular class a one-time
write class, but what of the situation I mentioned in the first message,
what you want to sort on is a priority of importance of tasks or something
where the priority is subject to change?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bill Mittenzwey <bm*********@hotmail.com> wrote:
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 be able to have sorted correctly in a sortedlist/hashtable... For that to work correctly, I need to override
Equals(). Fair enough, otherwise the sort would not really know what
represents the value of my object. Then the compiler tells me that I need to also override the GetHashCode. At first thought, it seems easy, just pass the call on to my describing property ( a stirng) and it would seem to work.

Yup.
Ah, but that seems to break the rules for GetHashCode. As I see it, I

need to guarantee that all objects which represent the same value return the same Hash Code (fine so far), and I need to guarantee that once an object has
supplied a hash code, it will never change. Conflicts start here. What if I have two objects which have there properties set to two different values, and so have reported back two different hash values. Then a program changes the property of one of the objects to be the same as the other. Now I have two objects which represent the same value, but with two different hash
codes.


As soon as types are mutable, you've got a problem whenever they're
stored in hash tables, sorted lists or whatever. Don't forget that
Equals will suddenly change as well - something that *was* Equal to
another thing possibly won't be any more.

Do you *definitely* need your property to be settable?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
Bill Mittenzwey <bm*********@hotmail.com> wrote:
After some thought, I would consider making that particular class a one-time
write class
Right - in which case, it would be best to put that one-time-write in
the constructor, or *very carefully* document that it should be set
*before* putting it in a hash table etc.
but what of the situation I mentioned in the first message,
what you want to sort on is a priority of importance of tasks or something
where the priority is subject to change?


Then I wouldn't make the priority part of the equality contract, I
suspect. I very rarely override Equals.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
In article <eb**************@TK2MSFTNGP10.phx.gbl>,
bm*********@hotmail.com says...
...but what of the situation I mentioned in the first message,
what you want to sort on is a priority of importance of tasks
or something where the priority is subject to change?


Then a hash table is probably not your best container, but be that as it
may, why not pull the item out of the list, change the priority and re-
insert it?

-- Rick

Jul 21 '05 #5

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

Similar topics

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...
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...
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: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
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()....
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.