我有一个 Person 类,我必须为其重写 Equals 和 GetHashCode 方法。如果名称匹配或电子邮件匹配,则两个人对象相等。使用相当有效的哈希函数来做这件事的好方法是什么?
class Person
{
string Name
string Email
public override Equals(object obj)
{
if (ReferenceEquals(obj, null))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj is Person)
{
Person person = (Person)obj;
return
(this.Name == person.Name)
|| (this.Email == person.Email);
}
return false;
}
public override GetHashCode()
{
// What's a good way to implement?
}
}