string, GetHashCode, and uniqueness

R

rh

hi all,
msdn says:
"the implementation of GetHashCode provided by the String class
returns unique hash codes for unique string values."
source:
http://msdn.microsoft.com/library/d...ml/frlrfsystemobjectclassgethashcodetopic.asp

sure, hashes must try to yield good distribution but
how could uniqueness be guarranteed in this case?
is it best effort? is it under certain conditions?

GetHashCode returns an int and so it is limited to a 32-bit
number. that's a big number but there are infinite
varieties of strings. just imagine a, aa, aaa, aaaa, aaaaa,
ad infinitum. and that's just using the letter 'a'.

thanks in advance for clarification,
rh
 
J

Jon Skeet [C# MVP]

rh said:
msdn says:
"the implementation of GetHashCode provided by the String class
returns unique hash codes for unique string values."
source:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
f/html/frlrfsystemobjectclassgethashcodetopic.asp

It's wrong.

For example, try "blair" and "brainlessness" - two different strings
which return the same hashcode.

Given the context, it *looks* like it's actually trying to say that
different string instances representing the same character data will
give back the same hashcode, but as it stands it's basically incorrect.

I'll email MSDN to let them know.
 
J

Jeff Gaines

It's wrong.

For example, try "blair" and "brainlessness" - two different strings
which return the same hashcode.


I think that example just proves it has more in built
intelligence than we might expect :))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top