比较对象用作字典的键(Comparing object used as Key in Diction

2019-07-30 12:35发布

我的课:

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>());
}

这该怎么做?

Answer 1:

如果你总是希望字典只对A和B的比较,你有两个选择。 无论是使用构造实现IEqualityComparer<TKey> ,把你的比较逻辑有,或有你的类实现IEquateable<T> 的GetHashCode和equals所以默认的比较会给你你正在寻找的结果。

如果你只想在你的一个情况的A和B比较,您将需要使用.Keys属性和LINQ的扩展方法包含 ,使您可以在一传IEqualityComparer<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());


Answer 2:

默认情况下,比较对象放在基于其散列码桶。 详细比较,然后(通过调用执行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;
        }
    }
}


Answer 3:

覆盖在MyClass的:

  • GetHashCode的方法

  • equals方法

要实现GetHashCode的方法,你可以XOR GetHashCodes从您的整数性能。

任选地重写ToString方法和实现IEquatable接口



文章来源: Comparing object used as Key in Dictionary