首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

如何小弟我定义operator ==后,使用一直报错

2012-04-04 
怎么我定义operator 后,使用一直报错?我定义了public static bool operator (MyShape lhs, object rhs

怎么我定义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);} 

热点排行