比较2个接口
interface I //: IEquatable<I>
{ }
class A : I
{
static public bool operator !=(A a, I i)
{
return !(a == i);
}
static public bool operator ==(A a, I i)
{
return true;
}
public override bool Equals(object obj)
{
if (obj is I)
return this == (I)obj;
else
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
class B : I
{
static public bool operator !=(B b, I i)
{
return !(b == i);
}
static public bool operator ==(B b, I i)
{
return false;
}
public override bool Equals(object obj)
{
if (obj is I)
return this == (I)obj;
else
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<I> iss = new List<I>();
A a = new A();
B b = new B();
iss.Add(a);
iss.Add(b);
if (iss[0] == iss[1])
Console.WriteLine("a == b");
else
Console.WriteLine("a != b");
if (iss[1] == iss[0])
Console.WriteLine("b == a");
else
Console.WriteLine("b != a");
}
}
输出是
a != b
b != a
我预期的
a == b
b != a
有人可以解释一下吗?
interface I //: IEquatable<I>
{ }
class A : I
{
static public bool operator !=(A a, I i)
{
return !(a == i);
}
static public bool operator ==(A a, I i)
{
return true;
}
public override bool Equals(object obj)
{
if (obj is I)
return this == (I)obj;
else
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
class B : I
{
static public bool operator !=(B b, I i)
{
return !(b == i);
}
static public bool operator ==(B b, I i)
{
return false;
}
public override bool Equals(object obj)
{
if (obj is I)
return this == (I)obj;
else
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<I> iss = new List<I>();
A a = new A();
B b = new B();
iss.Add(a);
iss.Add(b);
if (iss[0] == iss[1])
Console.WriteLine("a == b");
else
Console.WriteLine("a != b");
if (iss[1] == iss[0])
Console.WriteLine("b == a");
else
Console.WriteLine("b != a");
}
}
The output is
a != b
b != a
I expected
a == b
b != a
Could somebody can explain it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会在每个类的 Equals() 方法和 == 运算符中设置一个断点,并查看调用什么来计算每个表达式。这显然不是你所期望的。我的猜测是,因为
I
不会也不可能要求实现者公开==
运算符,因此当您比较两个I
时这样做时,运行时不会费心寻找重载的运算符,而是求助于 System.Object==
,它执行引用检查。I'd set a breakpoint in each class's Equals() method and == operator, and see what gets called to evaluate each expression. It's obviously not what you expect. My guess is that because
I
does not and cannot require implementors to expose a==
operator, when twoI
s are compared as you're doing, the runtime doesn't bother looking for overloaded operators and instead resorts to the System.Object==
, which performs a referential check.简单的答案:
您有一个
List
,并且您将两个I
相互比较。因为接口 I 没有(也不能)实现比较运算符,所以对象是通过引用进行比较的。相反,您可以使用抽象基类:
Simple answer:
You have a
List<I>
and you compare twoI
with each other. Becauseinterface I
does not (and cannot) implement a compare operator the objects are compared by reference.Instead you could use an abstract base class: