鉴于以下课程
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我已经覆盖了该Equals方法,因为它Foo代表了Foos表的一行.哪个是覆盖的首选方法GetHashCode?
覆盖为什么重要GetHashCode?
我有一个包含这个属性的类:
public class Person
{
public long Id { get; set; }
public string Name { get; set; }
public int? IdCountry { get; set; }
public virtual Country Country { get; set; }
public int? IdState { get; set; }
public virtual State State { get; set; }
}
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
public class State
{
public int Id { get; set; } …Run Code Online (Sandbox Code Playgroud)