String.GetHash code()返回的值不同不同、String、GetHash、code

2023-09-07 14:21:19 作者:隐身守候

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

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?

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

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.

更新:

如果你想知道什么,我终于实现了这个看看这个主题:Can在64位模式下运行时生成一个x86的哈希值?

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?

推荐答案

据的文档:

如果两个字符串对象是相等的,则GetHash code方法返回相同的值。然而,不存在用于每个唯一字符串值一个唯一的哈希$ C $的c值。不同的字符串可以返回相同的哈希code。

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.

因此​​,一些其他的效果必须在比赛的两次调用得出不同的结果。有一种说法是,你的通话之间切换平台,从86到64,反之亦然。

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.