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

tig*_*rou 4 c# linq equals gethashcode

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

GroupBy(x => x.SomeField, new FooComparer());
Run Code Online (Sandbox Code Playgroud)

我用作分组键的字段可以是null.因此,我不得不nullEquals()方法中添加一些检查:

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;
}
Run Code Online (Sandbox Code Playgroud)

问题是:我应该在GetHashCode()功能上做同样的事情吗?

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

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


编辑:

正如caerolus指出的那样,在GroupBy()实施过程中有一些特殊的检查.

我签到ILSpyGroupBy()实施了一个Lookup<TKey, TElement>

这是一个重要的功能:

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

ang*_*son 5

根据以下文件IEqualityComparer<T>.GetHashCode:

ArgumentNullException
类型为obj 引用类型,objnull.

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

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