为什么在重写Equals方法时重写GetHashCode很重要?
鉴于以下课程
public class Foo{ public int FooId { get; set; } public string FooName { get; set; } public override bool Equals(object obj) { Foo fooItem = obj as Foo; 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
?
慕侠2389804
白猪掌柜的
绝地无双