22

我的课:

public class myClass
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    public int D { get; set; }
}

和主要例子:

Dictionary<myClass, List<string>> dict = new Dictionary<myClass, List<string>>();
myClass first = new myClass();
first.A = 2;
first.B = 3;

myClass second = new myClass();
second.A = 2;
second.B = 3;
second.C = 5;
second.D = 6;

dict.Add(first, new List<string>());

if (dict.ContainsKey(second))
{
    //
    //should come here and update List<string> for first (and only in this example) key 
    //
}
else
{
    //
    //if myFirst object has difference vlues of A or B properties
    //
    dict.Add(second, new List<string>());
}

这该怎么做?

4

3 回答 3

37

如果您总是希望字典只比较 A 和 B,您有两个选择。要么使用实现的构造函数IEqualityComparer<TKey>并将你的比较逻辑放在那里,要么让你的类实现GetHashCode 和 Equals,这样默认的比较器就会给你你正在寻找的结果。IEquateable<T>

如果您只想在一种情况下比较 A 和 B,则需要使用 .Keys 属性和允许您传入 a的 Linq 扩展方法ContainsIEqualityComparer<T>。但是,当这样做时,您会失去使用字典的速度优势,因此请谨慎使用它。

public class MyClassSpecialComparer : IEqualityComparer<myClass>
{
    public bool Equals (myClass x, myClass y)
    { 
        return x.A == y.A && x.B == y.B 
    }

    public int GetHashCode(myClass x)
    {
       return x.A.GetHashCode() + x.B.GetHashCode();
    }


}


 //Special case for when you only want it to compare this one time
 //NOTE: This will be much slower than a normal lookup.
    var myClassSpecialComparer = new MyClassSpecialComparer();
    Dictionary<myClass, List<string>> dict = new Dictionary<myClass, List<string>>();
    //(Snip)
    if (dict.Keys.Contains(second, myClassSpecialComparer ))
    {
        //
        //should come here and update List<string> for first (and only in this example) key 
        //
    }

 //If you want it to always compare
    Dictionary<myClass, List<string>> dict = new Dictionary<myClass, List<string>>(new MyClassSpecialComparer());
于 2012-07-19T14:35:03.397 回答
11

默认情况下,比较会根据其哈希码将对象放入存储桶中。Equals如果两个哈希码相同,则执行详细比较(通过调用)。如果您的类既不提供GetHashCode也不实现相等,object.GetHashCode则将使用默认值——在这种情况下,不会将任何特定于您的类的内容用于值比较语义。只会找到相同的引用。如果您不想要这个,请实施GetHashCode并实施平等。

例如:

public class myClass
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    public int D { get; set; }

    public bool Equals(myClass other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return other.A == A && other.B == B && other.C == C && other.D == D;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (myClass)) return false;
        return Equals((myClass) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = A;
            result = (result*397) ^ B;
            result = (result*397) ^ C;
            result = (result*397) ^ D;
            return result;
        }
    }
}
于 2012-07-19T14:39:11.167 回答
4

在你的 myClass 中覆盖:

  • GetHashCode 方法

  • 等于方法

要实现 GetHashCode 方法,您只需对整数属性进行 XOR GetHashCodes 即可。

可选择重写 ToString 方法并实现 IEquatable 接口

于 2012-07-19T14:36:50.980 回答