GetHashCode() gives different results on different servers?

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/

Leave a Comment