为什么'397'用于ReSharper GetHashCode覆盖? [英] Why is '397' used for ReSharper GetHashCode override?
本文介绍了为什么'397'用于ReSharper GetHashCode覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b
public override int GetHashCode()
{
unchecked
{
int result =(Key!= null?Key.GetHashCode():0);
result =(result * 397)^(EditableProperty!= null?EditableProperty.GetHashCode():0);
result =(result * 397)^ ObjectId;
返回结果;
}
}
当然,我有一些我自己的成员,但我想知道的是为什么397?
- 编辑:所以我的问题会更好措辞,有什么'特殊'关于397以外的素数是质数?
解决方案
可能是因为397是足够大的主要元素,可以使结果变量溢出并混合散列的位,从而提供更好的散列代码分布。 397和其他同等数量的素数不同,没有什么特别的。
Like many of you, I use ReSharper to speed up the development process. When you use it to override the equality members of a class, the code-gen it produces for GetHashCode() looks like:
public override int GetHashCode()
{
unchecked
{
int result = (Key != null ? Key.GetHashCode() : 0);
result = (result * 397) ^ (EditableProperty != null ? EditableProperty.GetHashCode() : 0);
result = (result * 397) ^ ObjectId;
return result;
}
}
Of course I have some of my own members in there, but what I am wanting to know is why 397?
- EDIT: So my question would be better worded as, is there something 'special' about the 397 prime number outside of it being a prime number?
解决方案
Probably because 397 is a prime of sufficient size to cause the result variable to overflow and mix the bits of the hash somewhat, providing a better distribution of hash codes. There's nothing particularly special about 397 that distinguishes it from other primes of the same magnitude.
这篇关于为什么'397'用于ReSharper GetHashCode覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文