给然后按照类声明:无法执行重载方法
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();
}
}
我试图使用Employee.Equals(员工),但出于某种原因,它不工作:
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");
}
因为我将b转换为Employee,所以我期待Employee.Equals(Employee)被调用,因为它匹配更好的Employee.Equals(Object),但后者被调用。我究竟做错了什么?
更新的响应。 Equals的当前实施可能没有达到你的希望。 – 2012-07-10 16:48:09