Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetHashCode() gives different results on different servers?

Tags:

c#

gethashcode

I declared a C# line of code like so

int hashcode = "apple".GetHashCode();

On my computer, a computer at work, and a friend's computer, the result was 1657858284. On a development server, the result was 1548091822. Is there a way for me to tell the project to always make GetHashCode() yield 1657858284, regardless of which server it is on?

more notes At first, i noticed there was a difference in versions...the 1657858284 results came from .NET 3.5 and .NET 4.0. The 1548091822 came from .NET 2.0.

I then told visual studios 2010 to compile project as a .net 2.0 project, but it still gave me 1657858284.

like image 560
John Avatar asked May 24 '11 17:05

John


People also ask

Is string GetHashCode unique?

If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code. The hash code itself is not guaranteed to be stable.

When should we override the GetHashCode () method?

If you're implementing a reference type, you should consider overriding the Equals method if your type looks like a base type, such as Point, String, BigNumber, and so on. Override the GetHashCode method to allow a type to work correctly in a hash table.

Is GetHashCode unique C#?

NO! A hash code is not an id, and it doesn't return a unique value. This is kind of obvious, when you think about it: GetHashCode returns an Int32 , which has “only” about 4.2 billion possible values, and there's potentially an infinity of different objects, so some of them are bound to have the same hash code.

Is string GetHashCode deterministic?

The key point is that the hash codes are deterministic for a given program execution, that means the only time it'll be an issue is if you're saving the hash code outside of a process, and loading it into another one.


2 Answers

As others have noted, that is in accordance with the documentation. You must not rely on GetHashCode returning the same thing, ever. The only invariant you can rely upon is that it will return the same value on the same object in the same appdomain if the object has not been mutated in any way that changes its equality semantics. If any of those conditions are not met -- if the two objects are in different appdomains, or the object was mutated in a way that changes its equality semantics -- then you have no guarantee whatsoever that "identical" objects will return the same hash code.

The only thing you should be using a hash code for is to balance a hash table. Any other usage is "off label" and at your own risk. Don't do it. If you need a stable string hash that works across arbitrary boundaries then use an industry standard algorithm like SHA256 or something.

See my archive of articles about hashing issues for more details if this subject interests you:

http://blogs.msdn.com/b/ericlippert/archive/tags/hashing/

like image 64
Eric Lippert Avatar answered Oct 14 '22 12:10

Eric Lippert


It's possible that you're using 2 different versions of .Net. This behavior is noted on the MSDN article:
http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx
From the remarks:

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

like image 17
The Moof Avatar answered Oct 14 '22 11:10

The Moof