我在具有 List<T>
作为属性的自定义类中实现 IEquatable,如下所示:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<string> Dislikes;
public bool Equals(Person p)
{
if (p == null)
{
return false;
}
if (object.ReferenceEquals(this, p))
{
return true;
}
return this.FirstName == p.FirstName
&& this.LastName == p.LastName
&& this.Dislikes == p.Dislikes; //or this.Dislikes.Equals(p.Dislikes)
}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 23 + (this.FirstName ?? String.Empty).GetHashCode();
hash = hash * 23 + (this.LastName ?? String.Empty).GetHashCode();
hash = hash * 23 + this.Dislikes.GetHashCode();
return hash;
}
}
在尝试实现 Equals 和 GetHashCode 方法时,我担心 List 。具体来说, List <T>
.Equals 会评估其内容的相等性吗?List <T>
.GetHashCode 也是如此?