【发布时间】:2014-10-17 04:01:31
【问题描述】:
我已经通过question
我明白了,需要实现==、!=和Equals()。
public class BOX
{
double height, length, breadth;
// this is first one '=='
public static bool operator== (BOX obj1, BOX obj2)
{
return (obj1.length == obj2.length
&& obj1.breadth == obj2.breadth
&& obj1.height == obj2.height);
}
// this is second one '!='
public static bool operator!= (BOX obj1, BOX obj2)
{
return !(obj1.length == obj2.length
&& obj1.breadth == obj2.breadth
&& obj1.height == obj2.height);
}
// this is third one 'Equals'
public override bool Equals(BOX obj)
{
return (length == obj.length
&& breadth == obj.breadth
&& height == obj.height);
}
}
我假设,我已经正确编写了代码来覆盖 ==,!=,Equals 运算符。但是,我得到如下编译错误。
'myNameSpace.BOX.Equals(myNameSpace.BOX)' is marked as an override
but no suitable method found to override.
所以,问题是 - 如何覆盖上述运算符并摆脱此错误?
【问题讨论】:
-
不是像
public override bool Equals(object o)这样的Equals的签名吗? -
Resharper 建议像
return !(obj1 == obj2)一样编写!=,这应该利用您已经为==重载编写的内容。
标签: c# .net compiler-errors operator-overloading