Give then following Class declaration:
class Employee {
public string Name { get; set; }
public int Age { get; set; }
public override bool Equals(object obj)
{
Console.WriteLine("In Equals(Object)");
if (obj is Employee)
if (this.Name == (obj as Employee).Name && this.Age == (obj as Employee).Age)
return true;
else
return false;
else
return false;
}
public bool Equals(Employee obj)
{
Console.WriteLine("In Equals(Employee)");
return this.Equals(obj as Object);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
I'm trying to use Employee.Equals(Employee) but for some reason it doesn't work:
private static void CompareObjects(Object a, Object b)
{
if (a.Equals(b as Employee)) Console.WriteLine("a.Equals(b) returns true");
else Console.WriteLine("a.Equals(b) returns false");
}
As I'm casting b as Employee, I was expecting that Employee.Equals(Employee) would be called, since it matches the signature better then Employee.Equals(Object), but the latter is being called instead. What am I doing wrong?