gpt4 book ai didi

c# - 哈希码总是一样的?

转载 作者:太空宇宙 更新时间:2023-11-03 18:28:46 25 4
gpt4 key购买 nike

我想知道 HashCode 是否总是相同的?

例如:

String myString = "my super string";
Int myHashCode = myString.GetHashCode();

myHashCode 是否总是相同的值?在任何计算机上,任何时间,任何一切?

我可以相信这个值以将其用作自定义唯一标识(对于相同的对象类型)

最佳答案

不,该值可能会在计算机和基本系统版本之间发生变化。

您应该只依赖它在给定程序运行期间保持不变。

来自documentation :

The value returned by GetHashCode is platform-dependent. It differs on the 32-bit and 64-bit versions of the .NET Framework. It also can differ between versions of the .NET Framework.

Caution: A hash code is intended for efficient insertion and lookup in collections that are based on a hash table. A hash code is not a permanent value. For this reason:

  • Do not serialize hash code values or store them in databases.

Can I trust in this in value to use it as a custom unique identity?

这也行不通,即使是在单个程序运行期间,因为哈希码确实会发生冲突(不相等 对象的相同哈希码)。

再次引用文档:

  • Do not test for equality of hash codes to determine whether two objects are equal. (Unequal objects can have identical hash codes.) To test for equality, call the ReferenceEquals or Equals method.

关于c# - 哈希码总是一样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27435743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com