The easiest way is to simply quit:
if (a == (A) null)
Suppose you want to call an overloaded statement. If you want to compare for reference equality, you can use any of them:
if (a == (object) null) if (ReferenceEquals(a, null))
Personally, I would go with the second - I find it more explicit and, therefore, more clear.
My guess about why the reverse version works is that there is no overload ==(B, A) .
Personally, I would avoid overloading == like this anyway - it is very unusual for instances of different types to compare as equal, especially using ==. This is even worse if the operator was not overloaded symmetrically:
bool x = (a == b); bool y = (b == a);
If x and y can have different meanings here, you are really asking for a world of pain and hard-to-reach mistakes. Just don't do it ...
source share