String.GetHashCode() 返回不同的值

发布于 2024-10-08 08:43:24 字数 572 浏览 0 评论 0原文

为什么 GetHashCode() 对同一字符串返回不同的值?我无法描述如何复制这一点,但请相信这不是一个恶作剧,并且以下两行来自我的观察窗口在两个不同的时间:

"DDD.Events.Application.ApplicationReferenceCreated".GetHashCode() -1386151123 int
"DDD.Events.Application.ApplicationReferenceCreated".GetHashCode() 1858139950 int

这是怎么发生的?

我不知道这是否有帮助,但我正在 VS 2010 中的 .NET 4.0 上运行,并且正在调试 NServiceBus 应用程序。

更新:

如果您想知道我最终做了什么,请查看此线程:在 x64 模式下运行时能否生成 x86 哈希值?

Why is GetHashCode() returning a different value for the same string? I can't describe how to duplicate this, but trust that this is not a practical joke and that the two following lines came from my watch window at two separate times:

"DDD.Events.Application.ApplicationReferenceCreated".GetHashCode() -1386151123 int
"DDD.Events.Application.ApplicationReferenceCreated".GetHashCode() 1858139950 int

How could this happen?

I don't know if this helps, but I am running on .NET 4.0 in VS 2010 and I am debugging an NServiceBus application.

Update:

If you want to know what I ended up doing for this look at this thread: Can you generate an x86 hash value when running in x64 mode?

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

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

发布评论

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

评论(2

岁月如刀 2024-10-15 08:43:24

根据文档

如果两个字符串对象相等,则 GetHashCode 方法返回相同的值。但是,每个唯一的字符串值都没有唯一的哈希码值。不同的字符串可以返回相同的哈希码。

因此,必须有一些其他效果才能使这两个调用产生不同的结果。一种理论是您在调用之间切换了平台,从 x86 切换到 x64,反之亦然。

According to documentation:

If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.

Thus, some other effect must be in play for the two calls to give different results. One theory is that you switched platforms between the calls, from x86 to x64 or vice versa.

人疚 2024-10-15 08:43:24

您是否有可能从某处复制了该字符串?

我也有同样的问题。我复制了该值,不知何故,BOM 标头在第一个位置不可见。

尝试检查长度,您已经看到了差异。
您也可以逐字节检查。

Is it possible that you copied this string from somewhere?

I had the same problem. I copied the value ans somehow the BOM header is invisible on the first position.

Try checking the length and you see already a difference.
Also you could check byte-by-byte.

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