为什么“397"用于 ReSharper GetHashCode 覆盖? [英] Why is '397' used for ReSharper GetHashCode override?
问题描述
和你们中的许多人一样,我使用 ReSharper 来加快开发过程.当您使用它来覆盖类的相等成员时,它为 GetHashCode() 生成的代码生成如下所示:
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;
}
}
当然我有一些我自己的成员,但我想知道为什么是 397?
Of course I have some of my own members in there, but what I am wanting to know is why 397?
- 所以我的问题应该用更好的措辞来表述,除了它是素数之外,397 素数还有什么特别之处"?
推荐答案
可能是因为 397 是一个足够大的素数,可以导致结果变量溢出并在某种程度上混合散列的位,从而提供更好的散列代码分布.397 与其他同量级素数的区别没有什么特别之处.
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屋!