比较用作词典中的键的对象 [英] Comparing object used as Key in Dictionary

查看:131
本文介绍了比较用作词典中的键的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的课程:

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

主要示例:

 字典< 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))
{
//
//应该来这里并更新列表< string>对于第一个(仅在本例中)键
//
}
else
{
//
//如果myFirst对象有不同的vlues A或B属性
//
dict.Add(第二,新的List< string>());
}

如何做?

解决方案

如果您总是希望字典仅在A和B上进行比较,则有两种选择。 使用实现 IEqualityComparer< TKey> 并将您的比较逻辑放在那里,或让您的课程实施 IEquateable< T> GetHashCode和Equals ,所以默认比较器会给你所需的结果。 / p>

如果您只想在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();
}


}


//特殊情况下,当您只想比较一次
/ /注意:这将比正常查找慢得多。
var myClassSpecialComparer = new MyClassSpecialComparer();
字典< myClass,List< string>> dict = new Dictionary< myClass,List< string>>();
//(Snip)
if(dict.Keys.Contains(second,myClassSpecialComparer))
{
//
//应该来这里并更新列表<串GT;对于第一个(仅在本例中)键
//
}

//如果您希望它始终比较
Dictionary< myClass,List< string> > dict = new Dictionary< myClass,List< string>>(new MyClassSpecialComparer());


my class:

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

and main example:

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

How to do this?

解决方案

If you always want the dictionary only to compare on A and B, you have two options. Either use the constructor that implements IEqualityComparer<TKey> and put your comparison logic there, or have your class implement IEquateable<T> GetHashCode and Equals so the default comparer will give you the results you are looking for.

If you only want to compare on A and B in your one situation you will need to use the .Keys proporty and the Linq extension method Contains that allows you to pass in a 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());

这篇关于比较用作词典中的键的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆