本文介绍了GetHashCode扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读关于覆盖 GetHashCode()的所有关于StackOverflow的问题和解答之后,我写了下面的扩展方法,以便轻松方便地覆盖 GetHashCode )

  public static class ObjectExtensions 
{
private const int _seedPrimeNumber = 691;
private const int _fieldPrimeNumber = 397;
public static int GetHashCodeFromFields(this obj,params object [] fields){
unchecked {//取消选中以防止引发溢出异常
int hashCode = _seedPrimeNumber;
for(int i = 0; i< fields.Length; i ++)
if(fields [i]!= null)
hashCode * = _fieldPrimeNumber + fields [i] .GetHashCode );
返回hashCode;





我基本上只重构了代码,有人张贴在那里,因为我真的很喜欢它可以普遍使用)



我喜欢这样使用:

  public override int GetHashCode(){
return this.GetHashCodeFromFields(field1,field2,field3);
}

你看到这段代码有什么问题吗? b

解决方案

这看起来像是一个可靠的方法。



我唯一的建议是,如果你真的很担心它的性能,你可能想为几种常见情况添加通用版本(即可能有1-4个参数)。这样,对于那些对象(最有可能是小型的,键式复合对象),你不会有构建数组传递给方法,循环,任何通用值的装箱等的开销。调用语法将完全相同,但您将针对该情况运行稍微更优化的代码。当然,在决定是否值得维护权衡之前,我会对此进行一些性能测试。



类似这样:

  public static int GetHashCodeFromFields< T1,T2,T3,T4>(this obj,T1 obj1,T2 obj2,T3 obj3,T4 obj4){
int hashCode = _seedPrimeNumber;
if(obj1!= null)
hashCode * = _fieldPrimeNumber + obj1.GetHashCode();
if(obj2!= null)
hashCode * = _fieldPrimeNumber + obj2.GetHashCode();
if(obj3!= null)
hashCode * = _fieldPrimeNumber + obj3.GetHashCode();
if(obj4!= null)
hashCode * = _fieldPrimeNumber + obj4.GetHashCode();
返回hashCode;
}


After reading all the questions and answers on StackOverflow concerning overriding GetHashCode() I wrote the following extension method for easy and convenient overriding of GetHashCode():

public static class ObjectExtensions
{
    private const int _seedPrimeNumber = 691;
    private const int _fieldPrimeNumber = 397;
    public static int GetHashCodeFromFields(this object obj, params object[] fields) {
        unchecked { //unchecked to prevent throwing overflow exception
            int hashCode = _seedPrimeNumber;
            for (int i = 0; i < fields.Length; i++)
                if (fields[i] != null)
                    hashCode *= _fieldPrimeNumber + fields[i].GetHashCode();
            return hashCode;
        }
    }
}

(I basically only refactored the code that someone posted there, because I really like that it can be used generally)

which I use like this:

    public override int GetHashCode() {
        return this.GetHashCodeFromFields(field1, field2, field3);
    }

Do you see any problems with this code?

解决方案

That looks like a solid way to do it.

My only suggestion is that if you're really concerned about performance with it, you may want to add generic versions for several common cases (ie. probably 1-4 args). That way, for those objects (which are most likely to be small, key-style composite objects), you won't have the overhead of building the array to pass to the method, the loop, any boxing of generic values, etc. The call syntax will be exactly the same, but you'll run slightly more optimized code for that case. Of course, I'd run some perf tests over this before you decide whether it's worth the maintenance trade-off.

Something like this:

public static int GetHashCodeFromFields<T1,T2,T3,T4>(this object obj, T1 obj1, T2 obj2, T3 obj3, T4 obj4) {
    int hashCode = _seedPrimeNumber;
    if(obj1 != null)
        hashCode *= _fieldPrimeNumber + obj1.GetHashCode();
    if(obj2 != null)
        hashCode *= _fieldPrimeNumber + obj2.GetHashCode();
    if(obj3 != null)
        hashCode *= _fieldPrimeNumber + obj3.GetHashCode();
    if(obj4 != null)
        hashCode *= _fieldPrimeNumber + obj4.GetHashCode();
    return hashCode;
}

这篇关于GetHashCode扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:21