GetHashCode() 在不同的服务器上给出不同的结果?

发布于 2024-11-09 11:51:23 字数 389 浏览 2 评论 0原文

我像这样声明了一行 C# 代码

int hashcode = "apple".GetHashCode();

在我的计算机、工作计算机和朋友的计算机上,结果是 1657858284。在开发服务器上,结果是 1548091822。有没有办法让我告诉项目始终使 GetHashCode() 产生 1657858284,无论它在哪个服务器上?

更多注释 起初,我注意到版本之间存在差异......1657858284结果来自.NET 3.5和.NET 4.0。 1548091822 来自.NET 2.0。

然后我告诉 Visual Studios 2010 将项目编译为 .net 2.0 项目,但它仍然给了我 1657858284。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两仪 2024-11-16 11:51:23

正如其他人所指出的,这与文档一致。您绝对不能依赖 GetHashCode 返回相同的内容。您可以依赖的唯一不变量是,如果对象没有以任何方式改变其相等语义,它将在同一应用程序域中的同一对象上返回相同的值。如果不满足这些条件中的任何一个 - 如果两个对象位于不同的应用程序域中,或者对象以改变其相等语义的方式发生变异 - 那么你无法保证“相同”的对象将返回相同的哈希值代码。

您应该使用哈希码的唯一目的是平衡哈希表。任何其他用途均属于“标签外”,风险由您自行承担。不要这样做。如果您需要一个可以跨任意边界工作的稳定字符串哈希,请使用 SHA256 等行业标准算法。

如果您对此主题感兴趣,请参阅我有关哈希问题的文章存档以获取更多详细信息:

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

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/

苄①跕圉湢 2024-11-16 11:51:23

您可能正在使用 2 个不同版本的 .Net。
MSDN 文章中指出了此行为:
http://msdn.microsoft.com/en-us/library /system.object.gethashcode.aspx

从评论来看:

GetHashCode 方法的默认实现不保证不同对象的唯一返回值。此外,.NET Framework 不保证 GetHashCode 方法的默认实现,并且它返回的值在不同版本的 .NET Framework 之间是相同的。因此,此方法的默认实现不得用作哈希目的的唯一对象标识符。

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.

只涨不跌 2024-11-16 11:51:23

为了让您的自定义类返回稳定的哈希代码,您应该重写 GetHashCode() 方法,否则将使用 Object 类的 GetHashCode 方法,我认为这可能会有很大差异。 (甚至可能是特定于实例的)。

For your custom classes to return a stable hash code you should override the GetHashCode() method or else the GetHashCode method of the Object class will be used, which I think can vary a lot. (Might even be instance specific).

还给你自由 2024-11-16 11:51:23

如果您需要一个校验和,例如允许您快速验证传输中的数据完整性,只需通过具有适当位数的(加密)哈希来运行它,因为

- MD5
- SHA256
- SHA1
- fletcher

.Net 的 GetHashCode 并不意味着识别任何东西(无论如何,32 位很快就会导致冲突,这就是为什么你无论如何都不能用它来识别字符串。)

请注意,即使是上述四种也将允许冲突(但不太快);因此请务必将其用作校验和,而不是标识。

If you need to have a checksum that e.g. allows you to quickly verify data integrity in transport, simply run it through a (cryptographic) hash with appropriate number of bits, as

- MD5
- SHA256
- SHA1
- fletcher

.Net's GetHashCode is not meant to identify anything (32 bits is going to result in collisions very soon anyway, which is why you can't use it to identify string anyway.)

Note that even the above four will allow collisions (but less soon); so be sure to only use it as a checksum, not identification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文