有些数据结构用GetHashCode来做快速判断,但对‘比较’本身来说,GetHashCode并不是必要的。这些数据结构有Dictionary,HashSet等:class C
{
public static int i = 0;
public override int GetHashCode() { return i++; }
}static void Main(string[] args)
{
C obj1 = new C();
HashSet<C> hashSet = new HashSet<C>(new C[] { obj1, obj1 });
int count = hashSet.Count; // 2 (如果把GetHashCode override去掉,就是1)
}
{
public static int i = 0;
public override int GetHashCode() { return i++; }
}static void Main(string[] args)
{
C obj1 = new C();
HashSet<C> hashSet = new HashSet<C>(new C[] { obj1, obj1 });
int count = hashSet.Count; // 2 (如果把GetHashCode override去掉,就是1)
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货