[Mono-devel-list] Comparing against null

Ben Maurer bmaurer at ximian.com
Thu Apr 7 13:03:13 EDT 2005


On Thu, 2005-04-07 at 18:56 +0200, Joe Ante wrote:
> > 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).
> In our game engine we wrap C++ objects with small mono objects.
> The C++ representation might be destroyed. We use a lot of weak referencing
> and I want it to be straightforward to use.
> Comparing against null is the obvious way to do that.
> 
> When invoking a member method of a C# wrapper this will always generate an
> exception anyway. So i need some way to check if the actual C++ referenced
> object is still around.
> 
> > 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.
> In the general case i agree but i think for what we are doing it makes
> obvious sense to compare against nulll and return true if the C++
> representation is not around anymore, since it will generate exceptions if
> you use it anyway.

This usage lies somewhere in the gray, murkey watters between ugly and
wrong. I'd advise you find some other way to do what you want.

-- Ben




More information about the Mono-devel-list mailing list