如何覆盖Resharper的GetHashCode生成例程

时间:2018-03-27 14:33:03

标签: resharper resharper-2017

选择Generate > Equality members后,ReSharper会生成GetHashCode()方法与正文

public override int GetHashCode()
{
    unchecked
    {
        var hashCode = (int) Property1;
        hashCode = (hashCode * 397) ^ Property2;
        hashCode = (hashCode * 397) ^ Property3.GetHashCode();
...
        return hashCode;
    }
}

根据几篇文章
What is the best algorithm for an overridden System.Object.GetHashCode?
https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash

我想生成自己的GetHashCode()方法 (受第一个SO回答的启发)

public override int GetHashCode()
{
    unchecked
    {
        int hash = (int) 2166136261;
        hash = (hash * 16777619) ^ Property1.GetHashCode();
        hash = (hash * 16777619) ^ Property2.GetHashCode();
        hash = (hash * 16777619) ^ Property3.GetHashCode();
...
        return hash;
    }
}

有可能吗?怎么样?

0 个答案:

没有答案