Without overriding the Equals method, only a comparative comparison will be performed. You want to perform a comparison of values.
Override the method in your Employee class as follows:
public override bool Equals(Object emp) { // If parameter is null return false. if (emp == null) { return false; } // If parameter cannot be cast to Point return false. Employee e = emp as Employee; if ((System.Object)e == null) { return false; } // Return true if the fields match return (Name == emp.Name) && (Age == emp.Age); }
Then compare the objects as follows:
if(emp1.Equals(emp2)) { ... }
OR with a comparison operator:
if(emp1 == emp2) { ... }
Read more about MSDN here: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
source share