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

Confused by GetHashCode() documentation

Hi all,

I am somewhat confused by the Object.GetHashCode() documentation. What I am
looking for, is a hash code, which is bound to the _identity_ of an
object, not to it's _value_ (see other thread "Object identity").

In the documentation, the statement
"The hash function must return exactly the same value regardless of any
changes that are made to the object"
suggestes that the hash code is bound to an object _identity_ (that would
be useful for me).

But this contradicts to the statement
"If two objects of the same type represent the same value, the hash
function must return the same constant value for either object"
(saying that the hash code is bound to the object's _value_) and to the
examples in the documentation, e.g.
public struct Int32 {
public int value;
//other methods...
public override int GetHashCode() { return value; }
}

Which statement is right? Or where am I wrong? (I assume that the hash code
refers to the value, not the identity. This - unfortunately - would not
solve my problems, see other thread "Object identity").

- Stephan
Jan 22 '06 #1
1 1286
Stephan Keil <St**********@gmx.de> wrote:
I am somewhat confused by the Object.GetHashCode() documentation. What I am
looking for, is a hash code, which is bound to the _identity_ of an
object, not to it's _value_ (see other thread "Object identity").

In the documentation, the statement
"The hash function must return exactly the same value regardless of any
changes that are made to the object"
suggestes that the hash code is bound to an object _identity_ (that would
be useful for me).
The documentation is unfortunate, IMO. To me, it makes perfect sense
that if the value changes, the hash code should change too. You then
need to put a warning in Map etc that if you use a mutable object as a
key, you shouldn't change it afterwards or you might not be able to
find it again.

Of course, this partly depends on the whole business of System.Object
having a GetHashCode() method in the first place. Just using
IEqualityComparer would have probably been better, so that only those
classes which wished to advertise themselves as suuitable for hashing
had hash codes in the first place. (Classes could have implemented
providers for other classes, of course.)
But this contradicts to the statement
"If two objects of the same type represent the same value, the hash
function must return the same constant value for either object"
(saying that the hash code is bound to the object's _value_) and to the
examples in the documentation, e.g.
public struct Int32 {
public int value;
//other methods...
public override int GetHashCode() { return value; }
}

Which statement is right? Or where am I wrong? (I assume that the hash code
refers to the value, not the identity. This - unfortunately - would not
solve my problems, see other thread "Object identity").


Yes - it basically makes mutable objects return constants as their
hashcodes, which is plainly daft.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #2

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

Similar topics

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...
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
3
by: MuZZy | last post by:
Hi, Is there any guarantee that MD5 hashing algorithm implementation will not change in the next .NET version unlike what's happened to String.GetHashcode? Thank you, MuZZy
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: 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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
Oralloy
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.