« Notable Links | Main | Notable Links »

How to implement Equals in C#

Posted by: David Carroll

public override bool Equals(object obj)
{
if (obj == null) return false;
if (this.GetType() != obj.GetType()) return false;
// safe because of the GetType check
Customer cust = (Customer) obj;
// use this pattern to compare reference members
if (!Object.Equals(Name, cust.Name)) return false;
// use this pattern to compare value members
if (!Age.Equals(cust.Age)) return false;
return true;
}

Comments

Add New Comment