GetHashCode() 在不同的服务器上给出不同的结果?
我像这样声明了一行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人所指出的,这与文档一致。您绝对不能依赖 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/
您可能正在使用 2 个不同版本的 .Net。
MSDN 文章中指出了此行为:
http://msdn.microsoft.com/en-us/library /system.object.gethashcode.aspx
从评论来看:
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:
为了让您的自定义类返回稳定的哈希代码,您应该重写 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).
如果您需要一个校验和,例如允许您快速验证传输中的数据完整性,只需通过具有适当位数的(加密)哈希来运行它,因为
.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
.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.