Check for Null/Not Null with is null and is { }

Determine Which Mechanism Is Most Effective to Check for Null

It turns out there are numerous ways to check for null or not null, which begs the question, which should you use?

Not surprisingly, it depends.

Null/Not Null Mechanisms Table

Here’s a table describing the various mechanisms to check for null and their advantages and disadvantages.

Check For Code Checks For NullDescription
Is Null if(variable is null) return true; smiley face emoji This syntax supports static analysis such that later code will know whether variable is null or not.

neutral face emoji Requires C# 7.0 because it leverages type pattern matching.
Is Not Nullif(variable is { }) return falsesmiley face emoji This syntax supports static analysis such that later code will know whether variable is null or not.

neutral face emoji Requires C# 8.0 since this is the method for checking for not null using property pattern matching.

frowny face emoji Doesn’t produce a warning even when comparing against a non-nullable value type making the code to check pointless.
Is Not Nullif(variable is object) return falsesmiley face emoji Triggers a warning when comparing a non-nullable value type that could never be null

smiley face emoji This syntax works with C# 8.0’s static analysis, so later code will know that variable has been checked for null.

Checks if the value is not null by testing whether it is of type object.  (Relies on the fact that null values are not of type object.)
Is Nullif(variable == null) return truesmiley face emoji The only way to check for null before C# 7.0.

frowny face emoji However, because the equality operator can be overridden, this has the (remote) possibility of failing or introducing a performance issue.
Is Not Nullif(variable != null) return falsesmiley face emoji The only way to check for not null before C# 7.0.

frowny face emoji Since the not-equal operator can be overridden, this has the (remote) possibility of failing or introducing a performance issue.

Test Source Code

#nullable enable
string ? nullableText = "Inigo";
Assert.IsTrue(nullableText is object && nullableText is {});
nullableText = null;
Assert.IsFalse(nullableText is object || nullableText is {});

string notNullableText = "Inigo";
Assert.IsTrue(notNullableText is object && notNullableText is {});
notNullableText = null!; // Initentionally ignore the null assignment
Assert.IsFalse(notNullableText is object || notNullableText is {});

int ? nullableNumber = 42;
Assert.IsTrue(nullableNumber is object && nullableNumber is {});
nullableNumber = null;
Assert.IsFalse(nullableNumber is object || nullableNumber is {});
int notNullableNumber = 42;
Assert.IsTrue(
  // CS0183 - The given expression is always of the provided('object') type
  #pragma warning disable 0183 notNullableNumber is object #pragma warning restore 0183 &&
  notNullableNumber is {});
// notNullableNumber = null;  
// Error: Can't assing null to non-nullable value type

Ready for More?

Check out this video on the improvements and new features released in C# 9.0!

2 thoughts on “Check for Null/Not Null with is null and is { }”

  1. a) if ((object)variable == null)
    b) ReferenceEquals(variable, null)
    works in c# no matter which version
    a) for most analysers: they don’t understand that the type cast modifies the ==-operator

Leave a Reply

Your email address will not be published. Required fields are marked *