The perfect non-null test
Zohar Peled
Posted on November 13, 2019
Update: c# 9 has an even better option.
tl;dr; - if(x is object) {/* x is not null */}
Prior to c#8, all reference types where nullable in c#.
This means that we had to have some reliable way of making sure a reference is not null before using this reference, otherwise, we risk a NullRefereceException being thrown at run time.
Over the years, newer c# versions provided better ways of checking this.
We started with the naive
if(x != null)
{
// Do something with x here
}
This is naive because the !=
(and ==
) operator can be overloaded in c#, meaning that using this operator between a constant (null
) and any type might yield unexpected results, if that type happen to overload the !=
operator in a way that wouldn't produce the results you would normally expect.
c#6 introduced the null conditional operators: ?.
and ?[]
- which means that you can access an instance member (a method, indexer or property) of a type, if the instance is not null.
If the instance is null, the method will not be invoked, or the property or indexer will not be accessed - but the result of the entire expression would be null (which could be easily handled with the already existing null coalescing operator: ??
).
c#7 introduced pattern matching. Now you can check for null like this:
if(x is null)
{
// do stuff if it's null
}
else
{
// do stuff if it's not
}
A lot of c# developers, including myself, was missing a much more useful check: is not null.
Personally, I used to simply do this:
if(!(x is null))
{
// do stuff if x is not null
}
Which makes a rather ugly code, but at least it's safe.
Then I saw this tweet by Jared Parsons:
Jared Parsons@jaredpar@feslerj "x is null" is the perfect pairing with "x is object". One is the canonical null check, the other the canonical non-null check.18:00 PM - 10 Sep 2019
Which got me thinking: Why is checking x is object
the perfect non-null test in c#? So I've tried googling c# is object
and all sort of stuff along these lines, only to come up blank.
When that failed, the obvious option would be to go read the documentation of the is
operator more carefully - and sure enough - there it was:
When using the type pattern to perform pattern matching, is tests whether an expression can be converted to a specified type and, if it can be, casts it to a variable of that type.
expr is type varname
where
expr
is an expression that evaluates to an instance of some type,type
is the name of the type to which the result ofexpr
is to be converted, andvarname
is the object to which the result ofexpr
is converted if the is test is true.
So far no indication of a null test, but then, in the next line:
The is expression is true if
expr
isn'tnull
, and any of the following is true:
(going on listing the conditions).
Since any type in c# is compatible with object
, this means that the expression
x is object
can only evaluate to false if x
is null - which makes it the perfect non-null test.
Another benefit is that just like the compiler will not let you use x is null when x is a value type, it will issue a warning (CS0183) when attempting to use x is object if x is a value type:
warning CS0183: The given expression is always of the provided (‘object’) type
Posted on November 13, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.