我可以依赖 GetHashCode() 的值来保持一致吗?

编程入门 行业动态 更新时间:2024-10-28 09:26:30
本文介绍了我可以依赖 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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 01:25:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/793614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:GetHashCode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!