是否有内置的 IEqualityComparer 根据对象的 GetHashCode 值返回的值来比较对象?它很容易编写,但我更喜欢使用提供的类而不是自定义类。
当前代码:
private class HashComparer : IEqualityComparer<TKey>
{
private readonly Func<TKey, int> _Hasher;
public HashComparer (Func<TKey, int> hasher)
{
_Hasher = hasher;
}
public bool Equals (TKey x, TKey y)
{
// null supposed to throw, therefore no check
return _Hasher (x) == _Hasher (y);
}
public int GetHashCode (TKey obj)
{
return _Hasher (obj);
}
}