使用 C# 结构作为字典键的最佳方法是什么?

What is the most optimal way to use a C# struct as the key of a dictionary?

我有一个 C# 结构,用作标准字典集合中的键。我已经为它的 GetHashCode 和 Equals 编写了重写,但我有点不高兴 Equals 被赋予一个装箱对象而不是直接引用我的结构类型。

我可以做些什么来优化我对结构类型的字典的使用,以避免不必要的装箱操作?

(这不是过早的优化,而是完全合适的优化,非常感谢。)

您可以实现通用比较器:

public class MyStructComparer : IEqualityComparer<MyStruct>
{
    public bool Equals(MyStruct x, MyStruct y)
    {
        // ...
    }
    public int GetHashCode(MyStruct obj)
    {
        // ...
    }
}

然后将其用于 dictionary constructor:

var myStructDict = new Dictionary<MyStruct, string>(new MyStructComparer());

另一种方式是在MyStruct中实现IEquatable<MyStruct>,例如:

public struct MyStruct: IEquatable<MyStruct>
{
    public int Id;

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        return obj is MyStruct && Equals((MyStruct)obj);
    }

    public bool Equals(MyStruct other)
    {
        return this.Id == other.Id;
    }

    public override int GetHashCode()
    {
        return this.Id;
    }
}

然后可以用默认构造函数初始化字典:

var myStructDict = new Dictionary<MyStruct, string>();

你也可以试试算子重载。 检查下面的代码。

struct MyStruct
    {
        public int id;

        public static bool operator ==(MyStruct s1, MyStruct s2)
        {
            if (s1.id == s2.id)
                return true;
            return false;
        }

        public static bool operator !=(MyStruct s1, MyStruct s2)
        {
            if (s1.id == s2.id)
                return false;
            return true;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            return obj is MyStruct && Equals((MyStruct)obj);
        }

        public bool Equals(MyStruct other)
        {
            return this.id == other.id;
        }

        public override int GetHashCode()
        {
            return this.id;
        }
    }