日期:2014-05-18  浏览次数:20946 次

请问如何判断Dictionary的TKey相等
问题如下:
Dictionary 的key 和 value 都是自定义的类型。key是一个class . 定义如下

  public class HomePageChartEntity : IEquatable<HomePageChartEntity>
  {
  public string STimeBegin
  {
  get;
  set;
  }

  public string kPIID
  {
  get;
  set;
  }

  public bool Equals(HomePageChartEntity other)
  {
  if (
  && this.STimeBegin.Equals(other.STimeBegin)
  && this.kPIID.Equals(other.kPIID))
  {
  return true;
  }
  else
  {
  return false;
  }
  }

  }

那么声明一个Dictionary ,如下代码
Dictionary<HomePageChartEntity , string> text = new Dictionary<HomePageChartEntity , string>();

往里面添加元素

text.Add(new HomePageChartEntity {STimeBegin="123456",kPIID="test123456"} , "第1个键");
text.Add(new HomePageChartEntity {STimeBegin="123456",kPIID="test123456"} , "第2个键");
text.Add(new HomePageChartEntity {STimeBegin="123456",kPIID="test123456"} , "第3个键");

理论上,是不能添加进去的,因为是相等key ,但这段代码却可以添加到字典中去。
各位看看什么问题,谢谢!

代码手打的,难免有语法错误请见谅


------解决方案--------------------
判断key是否相同是调用了 object.GetHashcode()进行比较。
------解决方案--------------------
你的key是一个类型,每次new一个,指针当然不一样了,怎么会错误呢
可以重写 Equal和GetHashCode,Equal用来简单判断是否相等
------解决方案--------------------
呵呵,还要实现Object.Equals(Object)和 GetHashCode。

下面来自msdnhttp://msdn.microsoft.com/en-us/library/ms131187.aspx

If you implement IEquatable<T>, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable<T>.Equals method. 



从这篇文章里找到的线索。
http://www.haogongju.net/art/954904
------解决方案--------------------
这样就ok了,已经试过了
C# code

    public class HomePageChartEntity : IEquatable<HomePageChartEntity>
    {
        public string STimeBegin
        {
            get;
            set;
        }

        public string kPIID
        {
            get;
            set;
        }




        public bool Equals(HomePageChartEntity other)
        {
            if (this.STimeBegin.Equals(other.STimeBegin) && this.kPIID.Equals(other.kPIID))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override bool Equals(object obj)
        {
            HomePageChartEntity other = obj as HomePageChartEntity;
            if (other == null)
                return false;

            if (this.STimeBegin.Equals(other.STimeBegin) && this.kPIID.Equals(other.kPIID))
            {
                return true;
            }
            else
            {
                return false;
            } 
        }

        public override int GetHashCode()
        {
            return (STimeBegin + kPIID).GetHashCode();
        }

------解决方案--------------------
探讨
呵呵,还要实现Object.Equals(Object)和 GetHashCode。

下面来自msdnhttp://msdn.microsoft.com/en-us/library/ms131187.aspx

If you implement IEquatable<T>, you should also override the base class implementations o……