等于I有一个类实现接口,诸如这样的:重写在C#接口实现
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;
}
2个样本实现完全不同的功能 - 由您来决定你需要什么。一个相同的类型+相同的属性,花药 - 实现相同的接口+相同的属性。如果你希望'AClass'的实例等于其他类的类似对象(如[duck typing](http://en.wikipedia.org/wiki/Duck_typing)),则使用第二个类,第一个接近标准值“Equals”类型的行为 –