我可以依赖 GetHashCode() 的值来保持一致吗? [英] Can I depend on the values of GetHashCode() to be consistent?

查看:22
本文介绍了我可以依赖 GetHashCode() 的值来保持一致吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设使用相同的字符串值,GetHashCode() 的返回值是否保证一致?(C#/ASP.NET)

Is the return value of GetHashCode() guaranteed to be consistent assuming the same string value is being used? (C#/ASP.NET)

我今天将代码上传到服务器,但令我惊讶的是,我不得不重新索引一些数据,因为与台式计算机相比,我的服务器(win2008 64 位)返回的值不同.

I uploaded my code to a server today and to my surprise I had to reindex some data because my server (win2008 64-bit) was returning different values compared to my desktop computer.

推荐答案

如果我没记错的话,GetHashCode 在给定相同值的情况下是一致的,但不能保证在不同版本的框架中一致.

If I'm not mistaken, GetHashCode is consistent given the same value, but it is NOT guaranteed to be consistent across different versions of the framework.

来自关于 String.GetHashCode() 的 MSDN 文档:

From the MSDN docs on String.GetHashCode():

GetHashCode 的行为取决于它的实现,它可能会从公共语言运行时的一个版本更改为另一个版本.可能发生这种情况的一个原因是为了提高 GetHashCode 的性能.

The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. A reason why this might happen is to improve the performance of GetHashCode.

这篇关于我可以依赖 GetHashCode() 的值来保持一致吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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