GetHash code()给出了不同服务器上的不同的结果? [英] GetHashCode() gives different results on different servers?

查看:137
本文介绍了GetHash code()给出了不同服务器上的不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宣布code的像这样一个C#行

I declared a C# line of code like so

int hashcode = "apple".GetHashCode();

在我的电脑,在工作中一台电脑,一个朋友的电脑,结果是1657858284.在开发服务器,结果是1548091822.有我告诉工程总做方式GetHash code ()产量1657858284,无论哪个服务器是吗?

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?

更多音符
起初,我注意到有在版本的差异......在1657858284结果从.NET 3.5和.NET 4.0来了。该1548091822来自.NET 2.0。

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.

我再叫视觉工作室2010编译项目作为.NET 2.0的项目,但它还是给了我1657858284。

I then told visual studios 2010 to compile project as a .net 2.0 project, but it still gave me 1657858284.

推荐答案

这有可能是你使用2种不同版本的.NET。
此行为是注意到MSDN文章:

<一href=\"http://msdn.microsoft.com/en-us/library/system.object.gethash$c$c.aspx\">http://msdn.microsoft.com/en-us/library/system.object.gethash$c$c.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:

在GetHash code方法的默认实现不保证不同的对象唯一的返回值。此外,.NET框架不保证GetHash code方法的默认实现,它返回值将是不同版本的.NET Framework之间的相同。因此,这种方法的默认实现不能用作散列目的的唯一的对象标识符。

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.

这篇关于GetHash code()给出了不同服务器上的不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆