日期:2014-05-18  浏览次数:20928 次

怎么我定义operator ==后,使用一直报错?
我定义了
public static bool operator ==(MyShape lhs, object rhs).....
public static bool operator !=(MyShape lhs, object rhs).....

编译没有问题,怎么一和NULL比较就报错?
MyShape shape1 = null;
...
if (shape1 == null)...

------解决方案--------------------
C# code
public static bool operator ==(MyShape lhs, object rhs)
{
   if( object.ReferenceEqual(lhs, rhs) ) return true;
   if( object.ReferenceEqual(lhs,null) || object.ReferenceEqual(rhs,null) ) return false;
   ...
}
public static bool operator !=(MyShape lhs, object rhs)
{
   return !(lhs == rhs);
}