首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教怎么判断Dictionary的TKey相等

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

请问如何判断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……



[解决办法]
C# code
public override int GetHashCode(){   return (STimeBegin+kPIID).GetHashCode();}
[解决办法]
用(STimeBegin+kPIID).GetHashCode()做HashCode?那如果STimeBegin="ab",kPIID="c"和STimeBegin="a",kPIID="bc",这两个实例的HashCode岂不是一样了?
看一下MSDN就知道了,比较好的做法是用两个HashCode取"异或"运算
C# code
        public override int GetHashCode()        {            return this.STimeBegin.GetHashCode() ^ this.kPIID.GetHashCode();        }
[解决办法]
探讨

用(STimeBegin+kPIID).GetHashCode()做HashCode?那如果STimeBegin="ab",kPIID="c"和STimeBegin="a",kPIID="bc",这两个实例的HashCode岂不是一样了?
看一下MSDN就知道了,比较好的做法是用两个HashCode取"异或"运算
C# code

public override int G……

[解决办法]
探讨
判断key是否相同是调用了 object.GetHashcode()进行比较。

[解决办法]
探讨
引用:

引用:
呵呵,还要实现Object.Equals(Object)和 GetHashCode。

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

If you implement IEquatable<T>, you should also override the ……

[解决办法]
探讨

引用:
引用:

引用:
呵呵,还要实现Object.Equals(Object)和 GetHashCode。

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

If you implement IEquatable<T>, you should al……

[解决办法]
探讨
因为实现接口方法的Equals的参数类型不是object,它调用Equals的时候调用的还是bool Equals,而不是
override bool Equals这个方法。所以我觉得此处的override Equals没影响,你觉得呢?

附上一段MSDN的话:调用 Equals 方法并且 other 参数表示类型为 T 的强类型对象时。 (如果 other 的类型为 Object,则调用基方法 Object.Equals(Object)。 在这两种方法中,IEquatable<T>.Equals 提供稍好一点的性能。)

[解决办法]
另外一个有意思的地方是如果调用Equal,不需要实现IEquatable。 

下面的代码在vs2010中,if (a.Equals(b))会进入public bool Equals(HomePageChartEntity other), 而Dictionary调用Add时却不会进入,估计是.net framework底层实现的带来的问题。最好是实现IEquatable。 


C# code
 private void button2_Click(object sender, EventArgs e)        {            Dictionary<HomePageChartEntity , string> text = new Dictionary<HomePageChartEntity , string>();            HomePageChartEntity a = new HomePageChartEntity { STimeBegin = "123456", kPIID = "test123456" };            HomePageChartEntity b = new HomePageChartEntity { STimeBegin = "123456", kPIID = "test123456" };            if (a.Equals(b))                MessageBox.Show("aaa");            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个键");                                      } 

热点排行