在一些 C# 代码中,我使用linq GroupBy<TSource, TKey>()带有自定义IEqualityComparer<T>.
GroupBy(x => x.SomeField, new FooComparer());
我用作分组键的字段可以是null. 因此,我不得不null在方法中添加一些检查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()方法中提供了空键,GetHashCode()也永远不会用nullobj 参数中的对象调用。有人可以解释一下为什么吗?(这只是“纯粹的机会”,因为GroupBy()实施方式和我给它的元素的顺序吗?)
编辑 :
正如caerolus指出的那样,在GroupBy()实施过程中进行了一些特殊检查。
我签入ILSpy并GroupBy()实施了Lookup<TKey, TElement>
这是相关的功能:
internal int InternalGetHashCode(TKey key)
{
if (key != null)
{
return this.comparer.GetHashCode(key) & 2147483647;
}
return 0;
}