比较对象时等于VS GetHash code [英] Equals vs GetHashCode when comparing objects
问题描述
如果我们实现一个自定义的类实例的比较时,同时覆盖等于
和 GetHash code
属性?
Should we override both Equals
and GetHashCode
properties when implementing a custom class instances comparison?
在以下code我有一个类的集合。类 A
由比较 ID
,类乙
- 由 code
In the following code I have a collection of classes. The class A
is compared by the ID
, the class B
- by Code
.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<I> bars = new List<I>();
bars.Add(new A() { Id = 1, Code = "one A" });
bars.Add(new B() { Id = 1, Code = "one B" });
bars.Add(new A() { Id = 1, Code = "one A+" });
bars.Add(new B() { Id = 1, Code = "one B" }); // Code = "one B+"
var distictBars = bars.Distinct();
foreach (var item in distictBars)
{
Debug.WriteLine(item.Code);
}
}
}
interface I
{
string Code { get; set; }
}
class A : I, IEquatable<A>
{
public int Id { get; set; }
public string Code { get; set; }
public bool Equals(A other)
{
// this is the ??? comparison
return this.Id == other.Id;
//return this.Code == other.Code;
}
public override bool Equals(object obj)
{
if (obj is A)
return this.Equals(obj as A);
else
return object.ReferenceEquals(this, obj);
}
public override int GetHashCode()
{
// this is the wanted comparison
return this.Id;
}
}
class B : I, IEquatable<B>
{
public int Id { get; set; }
public string Code { get; set; }
public bool Equals(B other)
{
// this is the ??? comparison
return this.Id == other.Id;
}
public override bool Equals(object obj)
{
if (obj is B)
return this.Equals(obj as B);
else
return object.ReferenceEquals(this, obj);
}
public override int GetHashCode()
{
// this is the wanted comparison
return this.Code.GetHashCode();
}
}
}
的输出是:
one A
one B
万一评论 code =一个B +
输出
one A
one B
one B+
现在我问我自己,我应该覆盖哪些等于
在 B
类,如果它看来,这有对比较没有影响?
Now I ask myself what for should I override the Equals
in the B
class if it seems that this have no effect on comparison?
是 GetHas code()
压倒一切足以那种比较?
Is GetHasCode()
overriding sufficient for that kind of comparisons?
推荐答案
下面是你需要了解的等于
和 GetHash $的关系C $ç
。
Here's what you need to understand about the relationship between Equals
and GetHashCode
.
哈希codeS所使用的哈希表可以迅速找到桶,其中一个单元被预期存在。 如果元素是在两个不同的桶,假设是它们不能是相等的。
Hash codes are used by hashtables to quickly find a "bucket" in which an element is expected to exist. If elements are in two different buckets, the assumption is they cannot be equal.
这样做的结果是,你应该查看散列code,为了确定唯一性,作为一个快速的负的检查:即,如果两个物体有不同的hash codeS ,他们的没有的相同(不管它们的等于
方法返回)。
The outcome of this is that you should view a hash code, for purposes of determining uniqueness, as a quick negative check: i.e., if two objects have different hash codes, they are not the same (regardless of what their Equals
methods return).
如果两个物体具有的一样的散列code,它们将驻留在一个哈希表的同一个桶。 然后的其等于
方法将被调用,以确定相等。
If two objects have the same hash code, they will reside in the same bucket of a hash table. Then their Equals
methods will be called to determine equality.
所以 GetHash code
的必须的返回相同的值要被认为是相等的两个对象。
So GetHashCode
must return the same value for two objects that you want to be considered equal.
这篇关于比较对象时等于VS GetHash code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!