繁体   English   中英

如何实现多个 GetHashCode 方法?

[英]How to implement multiple GetHashCode methods?

我有一个定义复合键的接口:

public interface IKey : IEquatable<IKey>
{
    public bool KeyPart1 { get; }
    public uint KeyPart2 { get; }
    int GetHashCode(); // never gets called
}

我有一个对象(带有 ID),我想向其中添加复合键接口:

public class MyObject: IEquatable<MyObject>, IKey
{
    public MyObject(int i, (bool keyPart1, uint keyPart2) key) {
    {
        Id=i;
        KeyPart1 = key.keyPart1;
        KeyPart2 = key.keyPart2;
    }
    
    public int Id { get; }
    public bool KeyPart1 { get; }
    public uint KeyPart2 { get; }

    public bool Equals(MyObject other) => this.Id == other.Id;

    public override bool Equals(object other) => other is MyObject o && Equals(o);
    public override int GetHashCode() => Id.GetHashCode();

    bool IEquatable<IKey>.Equals(IKey other) => this.KeyPart1 == other.KeyPart1
                                                && this.KeyPart2 == other.KeyPart2;
    int IKey.GetHashCode() => (KeyPart1, KeyPart2).GetHashCode(); // never gets called
}

但是,当拥有这些对象的列表并尝试使用界面对它们进行分组时,分组失败:

var one = new MyObject(1, (true, 1));
var two = new MyObject(2, (true, 1));
var three = new MyObject(1, (false, 0));
var items = new[] { one, two, three };

var byId = items.GroupBy(i => i);
// result: { [one, three] }, { [two] } -- as expected

var byKey = items.GroupBy<MyObject, IKey>(i => i as IKey);

// result: { [one, two, three] } // not grouped (by 'id' or 'key')
// expected: { [one, two] }, { [three] }

我希望byId将按Id属性分组项目,而byKey将按Key属性分组项目。

但是, byKey根本没有分组。 似乎总是使用覆盖GetHashCode()方法而不是显式实现的接口方法。

是否有可能实现这样的事情,其中​​被分组的项目的类型决定了要使用的哈希方法(避免EqualityComparer )?

在将IEnumerable<IKey>对象传递给另一个需要IEnumerable<IKey>方法时,我注意到了这个问题。 我有几种不同的类型实现了IKey ,那些具有现有GetHashCode()方法的类型不起作用,而其他类型则起作用。

请注意这里的对象已被简化,我无法轻易更改接口(例如使用ValueTuple代替)。

等式中使用的GetHashCode()是:

  • 如果没有提供相等比较器,则通过object.GetHashCode()定义的
  • IEqualityComparer<T>.GetHashCode(T) ,如果提供了相等比较器

在您自己的接口上添加您自己的GetHashCode()方法没有任何作用,并且永远不会使用它,因为它不是框架/库代码知道的 API 的一部分。

所以,我会忘记IKey.GetHashCode()和其中一个(或两者):

  • 使MyObject.GetHashCode()提供您需要的功能,或
  • MyObject实例单独提供一个自定义的相等比较器

对于第二个选项,有接受IEqualityComparer<TKey>GroupBy重载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM