[Mono-devel-list] Comparing against null

Ben Maurer bmaurer at ximian.com
Thu Apr 7 09:57:08 EDT 2005


On Thu, 2005-04-07 at 11:12 +0200, Joe Ante wrote:
> Hi,
> 
> Is there a way to compare an object against null with a user defined
> operator?
> 
> In mono 1.0.4 i did get operator == called even if comparing against null.
> In mono 1.1.6 that doesn't happen anymore.
> Is this according to the standard? If it is how do i overload comparison
> against null?

Yes, that is correct. On csc x == null *always* does a pointerwise
comparison. I made mcs do this. In some ways it is an optimization (the
user one will at the very least require a function call), in others a
correctness (though that seems very pendantic -- I am not sure if there
are any real life examples where it makes a difference).

If you absolutely need it, have a static readonly variable with the
value of null. However, I'd advise that your equals impl only return
`true' for the equality of null to another null.

-- Ben




More information about the Mono-devel-list mailing list