Search code examples
.netgethashcode

Is long.GetHashCode reliable cross environments


Will the value that comes out of long.GetHashCode() be reliably the same across different .NET Framework-versions, OS-versions, processor-architecture and so on?

The question is based on other questions that mentions different results on different servers.

// Will I be the same everywhere?
var hash = 2170881568869167279.GetHashCode();

Bonus: Does the same go for int and uint?


Solution

  • Per the contract of GetHashCode, it is not even required to be the same on the same machine in two different processes:

    the default implementation of this method must not be used as a unique object identifier for hashing purposes.

    For certain types it could be - and probably is - implemented in a way that will always return the same hash code, even on different machines. But that is an implementation detail you should not rely on - it could change without notice.

    Furthermore, two different objects can legally have the same hash code. In your example with long, on average you will have each hashcode long.MaxValue / int.MaxValue times when you create the hashcode for all values from long.MinValue to long.Maxvalue.

    Conclusion:
    No, it is not a reliable way to identify an instance of an object.

    When dealing with numbers, you could simply use the number itself or use a real hashing algorithm.