Back to Writing
NOTEScsharpnull-checkingpattern-matchingbest-practices
C# - Null Checking with "is null"
August 5, 2020•Updated Feb 17, 2026
In C#, use the "is null" check to verify null values.
Syntax (C# 9.0+):
if (obj is null) {
// Handle null case
}
Older syntax:
if (obj == null) {
// Handle null case
}
The "is null" pattern is more idiomatic in modern C#.
Example:
string name = GetName();
if (name is not null) {
Console.WriteLine(name);
}
Benefits:
- More readable than == null
- Aligns with other pattern matching features
- Potential for future optimizations