我可以使用的GetHashCode()的所有字符串比较? [英] Can i use GetHashCode() for all string compares?

查看:118
本文介绍了我可以使用的GetHashCode()的所有字符串比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缓存基于对象的某些搜索结果搜索和一些搜索设置



不过:这将创建一个相当长的缓存键,我想我倒是为它创建一个快捷方式,我想我会使用的GetHashCode()吧。



所以,我想知道,是否的GetHashCode()总是产生不同的号码,甚至当我有很长的字符串或者这只是不同:在A<代替'ä' / p>

我尝试了一些字符串,它的显得的答案是肯定的,但不理解的GetHashCode()行为并没有给我的真实感觉我是对的。



和,因为它是这些东西时会弹出还不准备你一(客户正在考虑为错误的搜索缓存结果)我想是的......



编辑:如果MD5会的工作,我可以改变我的代码不使用该GetHashCode的ofcourse,目标是获得一个短(ER)的字符串比原来的(> 1000个字符)


解决方案

您不能指望的GetHashCode()是独一无二的。



有是调查可在碰撞发生的可能性的优秀文章< A HREF =http://kenneththorman.blogspot.com/2010/09/c-net-equals-and-gethashcode.html相对=nofollow> http://kenneththorman.blogspot.com/2010/09/ C-NET-等于-和gethashcode.html 。该调查结果是调用GetHashCode()方法的最小数返回相同的哈希码不同的字符串是经过565次迭代,得到一个哈希码碰撞前的迭代次数最多的是296390的迭代。



所以,你可以理解的GetHashCode 的实施合同,以下是从MSDN文档中摘录的的 Object.GetHashCode()



一个哈希函数必须具有以下属性:




  • 如果两个比较对象为相等,每个对象GetHashCode方法必须返回相同的值。但是,如果两个对象不比较结果相等,两个对象的GetHashCode方法不必返回不同的值。


  • 一个对象GetHashCode方法只要没有修改,确定对象的equals方法的返回值对象的状态必须一致地返回相同的散列码。注意,这是真实的,只有一个应用程序的当前执行,并且如果该应用程序被再次运行可以返回不同的散列码


  • 有关的获得最佳性能,哈希函数必须为所有输入生成随机分布。




C#编译器团队埃里克利珀解释理处的的GetHashCode 实施细则=http://ericlippert.com/2011/02/28/guidelines-and-rules-for -gethashcode /相对=nofollow> http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/


i want to cache some search results based on the object to search and some search settings.

However: this creates quite a long cache key, and i thought i'd create a shortcut for it, and i thought i'd use GetHashCode() for it.

So i was wondering, does GetHashCode() always generate a different number, even when i have very long strings or differ only by this: 'ä' in stead of 'a'

I tried some strings and it seemed the answer is yes, but not understanding the GetHashCode() behaviour doesn't give me the true feeling i am right.

And because it is one of those things which will pop up when you're not prepared (the client is looking at cached results for the wrong search) i want to be sure...

EDIT: if MD5 would work, i can change my code not to use the GetHashCode ofcourse, the goals is to get a short(er) string than the original (> 1000 chars)

解决方案

You CANNOT count on GetHashCode() being unique.

There is an excellent article which investigates the likelihood of collisions available at http://kenneththorman.blogspot.com/2010/09/c-net-equals-and-gethashcode.html . The findings were that "The smallest number of calls to GetHashCode() to return the same hashcode for a different string was after 565 iterations and the highest number of iterations before getting a hashcode collision was 296390 iterations. "

So that you can understand the contract for GetHashCode implementations, the following is an excerpt from MSDN documentation for Object.GetHashCode():

A hash function must have the following properties:

  • If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

  • The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.

  • For the best performance, a hash function must generate a random distribution for all input.

Eric Lippert of the C# compiler team explains the rationale for the GetHashCode implementation rules on his blog at http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/ .

这篇关于我可以使用的GetHashCode()的所有字符串比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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