This can make a difference in three cases.
Firstly, a typo of condition = null valid in if . This is more common in C-style languages ββthat allow null in if (evaluated as false ), which is most of them, but not C #.
In C #, you can create a type that has this effect:
public class Test { public static bool operator true(Test x) { return true; } public static bool operator false(Test x) { return false; } } void Main() { Test test = new test(); if (test = null) { Console.WriteLine("!"); } }
Not so many times that overloading these operators makes sense, especially since .Net 2.0 introduced generics (it had more value for types like SqlBoolean , which allowed us to indicate true , false or null in the way that we will now use bool? ).
So this case is pretty marginal in C #.
The other seems to be if there is an implicit conversion to bool or to a type that, in turn, implements the true and false operators:
void Main() { Test test = new Test(); if (test = null) { Console.WriteLine("!"); } } public class Test { public static implicit operator bool(Test x) { return true; } }
Implicit operators should be avoided for several reasons, but this is slightly more likely than the first example, although it still does not come close to the general.
Another one if == overloaded asymmetrically:
public class Test { public static bool operator == (Test x, Test y) { return ReferenceEquals(x, null); } public static bool operator !=(Test x, Test y) { return !(x == y); } } void Main() { Test test = new Test(); if (test == null) { Console.WriteLine("This won't print."); } if (null == test) { Console.WriteLine("This will print."); } }
But since asymmetric == always an error, it depends on the fact that an error in the statement has any effect. This probably happens a little more often than in the first case, but it should be fixed when it happens, so itβs even less relevant.
Thus, although this may have an effect on C #, cases are rare and are mainly based on the fact that someone did something they shouldn't.
So this is basically a matter of style. People who set null first tend to choose it from languages ββwhere it matters more.