C# is not null: Pattern Matching for Null Checks
The C# is not null pattern is a concise, type-safe way to check for null values and avoids overloaded operator behavior. This article shows how it differs from != null and when to use it.

When you write a null check in C#, the expression x is not null has become a common alternative to x != null. The pattern-based check is more than a stylistic choice: it bypasses overloaded operators and works consistently across reference types and nullable value types. This article explains how the is not null pattern behaves, where it fits in the language, and how to use it effectively.
The is not null Pattern in C#
The is not null pattern is part of C# 9's extended pattern matching. It checks whether an expression is not null using the constant pattern null. The syntax is straightforward:
if (name is not null)
{
Console.WriteLine(name.Length);
}This is equivalent to name != null for most practical purposes, but the pattern-based version has a subtle advantage: it does not invoke any user-defined != operator. If the type of name overloads != to perform custom logic, name != null might not behave as a simple reference comparison. The is not null pattern always performs a direct null check, regardless of operator overloading.
Why is not null Differs from != null
The most important difference is operator overloading. Consider a type that overloads == and != to compare by value:
public class Money
{
public decimal Amount { get; set; }
public string Currency { get; set; }
public static bool operator ==(Money left, Money right) =>
left?.Amount == right?.Amount && left?.Currency == right?.Currency;
public static bool operator !=(Money left, Money right) =>
!(left == right);
}If you write money != null, the overloaded operator runs. If the operator is not written defensively, it can return an unexpected result or throw when it dereferences a null operand. The is not null pattern avoids this entirely because it is a pattern match, not an operator call. It checks the underlying reference or nullable state directly.
This makes is not null the safer choice when you are working with types that overload comparison operators. It also makes the intent clearer: you are checking for a null state, not performing a semantic equality comparison.
Using is not null in Property Patterns
The is not null pattern composes with other patterns. A common use is the property pattern, which lets you check nested properties without explicit null checks:
if (customer is { Address: not null })
{
// customer is not null, and Address is not null here
}This is equivalent to customer?.Address != null, but it is more readable and keeps the null check close to the property access. You can combine multiple property checks:
if (order is { Customer: not null, Total: > 100 })
{
// Process large order from a known customer
}The compiler understands the pattern and may even provide flow analysis to avoid redundant checks. This style is particularly useful in switch expressions and if statements where you want to validate several fields at once.
Null Checks in Expressions and Conditions
The is not null pattern can be used wherever a Boolean expression is expected, not just in an if statement. For example, it works in conditional expressions and switch expressions:
string label = name is not null ? name.ToUpperInvariant() : "unknown";
string category = value switch
{
not null when value.Length > 10 => "long",
not null => "short",
null => "missing"
};In a switch expression, not null is a pattern that matches any non-null value. This is more concise than writing _ when value != null. The pattern also works with nullable value types: int? maybe = 5; if (maybe is not null) correctly checks whether the underlying value exists, without boxing or calling HasValue explicitly.
Performance and Compiler Behavior
From a runtime perspective, is not null is a simple null check. For reference types, the generated IL is a direct null comparison, not a call to an overloaded != operator. For nullable value types, the pattern translates to a check of the HasValue property, which is also efficient.
The main performance benefit is that you avoid invoking overloaded operators. If a type's != operator performs complex logic, using is not null eliminates that overhead. There is no boxing, no virtual call, and no additional allocation. The pattern is also predictable: it always means "the reference is not null" or "the nullable has a value," regardless of the type's custom equality semantics.
Because the compiler treats is not null as a pattern, it can perform flow analysis. For example, after if (x is not null), the compiler knows x is not null in the true branch, which can help avoid nullability warnings in code that uses nullable reference types.
Compatibility and Language Versions
The is not null pattern requires C# 9 or later. It is a compiler feature, so you can use it in projects that set the language version to C# 9 or newer; it is not tied to a particular .NET runtime. A .NET Core 3.1 project with a recent SDK can enable it by setting the language version. If you are working with an older C# compiler, use != null or ReferenceEquals instead.
For nullable value types, the pattern behaves the same in any version that supports C# 9. The nullable reference type annotations are a separate compiler feature and are not needed to use is not null.
When migrating existing code, you can replace x != null with x is not null safely for most types. The only exception is if you intentionally rely on an overloaded != operator to perform a custom null check, which is rare and usually a design flaw.
Common Pitfalls and Edge Cases
The is not null pattern is straightforward, but a few edge cases can surprise developers.
For nullable value types, int? x = null; x is not null is false, and x is null is true. This is intuitive. The pattern checks the nullable state; it does not provide the stored value. If you need to extract the value, use .Value, the ?? operator, or a type pattern such as maybe is int value.
Another edge case is the not pattern combined with type patterns. For example, x is not string matches any value that is not a string, including null. This is different from x is not null, which only checks for null. Mixing these up can lead to logical errors.
Finally, when a nullable value type is the input to a switch expression, not null matches the case where HasValue is true, but it does not change the type of the value available in that arm. For example, maybe remains an int? inside a not null arm. If you need the underlying int, use a type pattern such as int value instead of relying on not null to convert it.
In practice, is not null is a reliable, readable, and safe way to perform null checks. It avoids operator overloads, works consistently across types, and integrates with modern C# pattern matching features. Use it as your default null check in code that targets C# 9 or later.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.