读书人

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

发布时间: 2012-04-04 16:38:51 作者: rapoo

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

读书人网 >C#

热点推荐