我有一个实现接口的类,例如:
interface IInterface
{
string PropA { get; }
string PropB { get; }
}
class AClass : IInterface
{
string PropA { get; protected set; }
string PropB { get; protected set; }
}
平等是根据 PropA 和 PropB 确定的。当覆盖 AClass 的 Equals 方法时,我是否应该尝试将 obj 转换为 AClass,如下所示:
public override bool Equals(object obj)
{
AClass other = obj as AClass;
return other != null
&& AClass.PropA == other.PropA
&& AClass.PropB == PropB;
}
或者我应该尝试将 obj 转换为 IInterface,如下所示:
public override bool Equals(object obj)
{
IInterface other = obj as IInterface;
return other != null
&& AClass.PropA == other.PropA
&& AClass.PropB == PropB;
}