给定以下课程
public class Foo
{
public int FooId { get; set; }
public string FooName { get; set; }
public override bool Equals(object obj)
{
Foo fooItem = obj as Foo;
if (fooItem == null)
{
return false;
}
return fooItem.FooId == this.FooId;
}
public override int GetHashCode()
{
// Which is preferred?
return base.GetHashCode();
//return this.FooId.GetHashCode();
}
}
我已经覆盖了该Equals
方法,因为Foo
它代表了Foo
s 表的一行。哪个是覆盖的首选方法GetHashCode
?
为什么覆盖很重要GetHashCode
?