Hash quality and stability of String.GetHashCode() in .NET?

前端 未结 5 462
日久生厌
日久生厌 2020-12-31 04:03

I am wondering about the hash quality and the hash stability produced by the String.GetHashCode() implementation in .NET?

5条回答
  •  抹茶落季
    2020-12-31 04:31

    I can't give you any details about the quality (though I would assume it is pretty good given that string is one of the framework's core classes that is likely to be used as a hash key).

    However, regarding the stability, the hash code produced on different versions of the framework is not guaranteed to be the same, and it has changed in the past, so you absolutely must not rely on the hash code being stable between versions (see here for a reference that it changed between 1.1 and 2.0). In fact, it even differs between the 32-bit and 64-bit versions of the same framework version; from the docs:

    The value returned by GetHashCode is platform-dependent. For a specific string value, it differs on the 32-bit and 64-bit versions of the .NET Framework.

提交回复
热议问题