编程语言
首页 > 编程语言> > c# – GetHashCode()方法应该注意作为参数给出的null值吗?

c# – GetHashCode()方法应该注意作为参数给出的null值吗?

作者:互联网

在一些C#代码中,我使用linq GroupBy< TSource,TKey>()方法和自定义IEqualityComparer< T>.

GroupBy(x => x.SomeField, new FooComparer());

我用作分组键的字段可以为空.因此,我必须在Equals()方法中添加一些空检查:

public bool Equals(Foo x, Foo y)
{
    if (x == null && y == null)
       return true;
    else if (x == null && y != null)
       return false;
    else if (x != null && y == null)
       return false;
    else 
       return x.Id == y.Id;
}

问题是:我应该在GetHashCode()函数中做同样的事情吗?

public int GetHashCode(Foo obj)
{
    if (obj == null)          //is this really needed ?
       return default(int);   //
    else
       return obj.Id;
}

我不明白的东西:即使使用GroupBy()方法中提供的null键,也不会在obj参数中使用null对象调用GetHashCode().有人可以解释一下为什么吗? (它只是“纯粹的机会”,因为GroupBy()的实现方式和我给它的元素的顺序?)

编辑:

正如caerolus指出的那样,在GroupBy()实现中进行了一些特殊检查.

我检查了ILSpy,GroupBy()实现了Lookup< TKey,TElement>

这是一个重要的功能:

internal int InternalGetHashCode(TKey key)
{
    if (key != null)
    {
        return this.comparer.GetHashCode(key) & 2147483647;
    }
    return 0;
}

解决方法:

根据the documentation of IEqualityComparer<T>.GetHashCode

ArgumentNullException
The type of obj is a reference type and obj is null.

所以这是该界面合同的一部分,因此你应该关心.如果obj为null,则通过抛出ArgumentNullException来实现它.

您应该始终坚持使用界面,即使您怀疑或可以证明代码永远不会触及您不关心的部分.稍后的更改可能会引入依赖于该行为的代码.

标签:c,linq,equals,gethashcode
来源: https://codeday.me/bug/20190620/1244532.html