最佳答案
给定以下类
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
很重要?