if
expressions have to be boolean. Say I have a string:
string test = null;
if (test)
won’t compile because test
is not a bool. It just returns the string reference. So, to check for null, you actually have to check for null:
if (test != null)
is an expression that does return a bool, and so compiles, and is a standard way of checking for null.
solved Why use != null in if statements?