July 21, 2011

Implementing Equals in C#

For current team:


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;
}

Source: Ted Graham on .NET

0 comments:

Post a Comment