Chapter 3: Null safety
Tony Hoare—the inventor of the null reference—once called it “the Billion Dollar Mistake.” A decision that seemed harmless at the time ended up causing many crashes and security issues, and it made some bugs painfully hard to diagnose. In particular, when a possibly null reference passes through several methods, the place where NullReferenceException occurs is often not where the problem began.
Since its earliest versions, C# has steadily evolved its approach to handling nulls: C# 2.0 brought nullable value types (T?), later releases added null-related operators (??, ?.), and C# 8 introduced Nullable Reference Types (NRT). Each step makes nullability more explicit, more traceable, and easier for the compiler to check.
This chapter walks through modern C# null safety so you can reduce NullReferenceException incidents and move more null handling from “debug it later” to “catch it earlier.”
3.1 Why do we need null safety?
Early C# allowed variables of any reference type to hold null, but had no dedicated nullability annotations for the compiler to track this. For example:
In a large project, if the value of name comes from another method or external data, its declaration alone gives little indication of whether it can be null.
Starting with C# 8, Nullable Reference Types (NRT) let us express that distinction explicitly. Modern .NET project templates enable NRT by default. With NRT enabled, string means the value should not be null, whereas string? permits null.
This warning gives us an opportunity to fix the problem before running the code. NRT does not automatically insert runtime null checks, however. If you ignore the warning and run this code, it still throws NullReferenceException.
Unless otherwise noted, the remaining examples assume NRT is enabled. Section 3.4 explains how to configure and use it.
Early approaches
Before Nullable<T>, handling “missing values” (such as database columns) was awkward. Developers often used magic numbers to represent “no data,” such as:
This is error-prone and makes code harder to understand and maintain.
3.2 Nullable value types
C# 2.0 introduced nullable value types, implemented as the generic System.Nullable<T>, so value types can represent a “no value” state.
Think of Nullable<T> as a gift box:
- It might contain a value.
- Or it might be empty (
null).

Syntax: the concise T?
You can write Nullable<int>, but C# provides cleaner syntactic sugar:
In modern C# code, prefer the T? form.
Assigning and reading values
Suppose we need to record a score that may not have been entered yet. Declare score as int?, using null to represent a missing score instead of choosing a special number.
If score is null, score == 25 evaluates to false, but accessing score.Value throws InvalidOperationException. Check that a value exists before retrieving it through Value.
Under the hood: Nullable<T>
Understanding the internal structure helps explain its behavior. Nullable<T> is a small struct with two fields. Conceptually, it looks like this (simplified):
There are three points to keep in mind:
int?uses a bit more memory thanintbecause it must store an extra “has value” flag.HasValueindicates whether a value is present;Valuecan retrieve the stored value only when it istrue.- Assigning
nullto a nullable variable makes itsHasValuefalse.
Checking whether it has a value
Continuing the score example, use HasValue or a comparison with null to retrieve a score only when one exists:
Both approaches behave the same in this int? example. Choose whichever your team finds easier to read.
Converting to and from non-nullable types
Converting a value type to its nullable counterpart is implicit (and safe). The reverse operation requires an explicit cast because it introduces the risk of failure:
An explicit conversion tells the compiler: “I know what I’m doing”—and you accept the responsibility for the runtime risk.
Arithmetic with nullable values
In an arithmetic expression, if one operand is null, the result is null. In practice, this behavior is often called null propagation:
This matches intuition: if one of the operands is unknown (null), the overall result is naturally unknown as well.
In the C# language specification, this capability is implemented under the hood via lifted operators. A “lifted” operator means the compiler automatically expands an operator that normally works on a value type (like int) so it can also work with the corresponding nullable type (like int?).
Thanks to this mechanism, you can directly mix nullable and non-nullable operands:
Even if one operand (n) is a regular non-nullable type, the overall result of the expression is still nullable. This preserves type consistency and saves you from writing tedious casts in everyday expressions.
Source code: DemoNullableValueType
Food for thought
Suppose a quiz score has not been entered yet (int? score = null;). What is the result of adding a ten-point bonus using score + 10? What if you treat the missing score as zero instead?
Predict the results, then run the code to check them. For a real grading system, consider how these two ways of handling missing data affect the records. The next section introduces ?? for supplying a default.
3.3 Null operators: Making null handling concise
To keep code both concise and safe, C# provides several operators specifically designed for null handling:
- Null-coalescing operator (
??): Provides a fallback value and prevents nulls from flowing downstream. - Null-coalescing assignment (
??=): Simplifies lazy initialization and “assign default if null” patterns. - Null-conditional operators (
?.and?[]): Skip subsequent member or indexer access when the receiver is null. - Null-conditional assignment (C# 14): Assigns only when the receiver is not null.
Null-coalescing operator (??)
The null-coalescing operator is ??. Its meaning is: “if the left operand is not null, use it; otherwise use the right operand.”
It’s essentially Plan B: if Plan A is null, fall back to Plan B.
Example:
In this example, the fallback value 5 is an int, so the result of the expression cannot be null. This is equivalent to:
int y = (x != null) ? x.Value : 5;Practical examples:
Null-coalescing assignment operator (??=)
Starting with C# 8, you can use the null-coalescing assignment operator ??=. It means: “if the left operand is null, assign the right operand to it.”
Code like this:
This can be reduced to a single line:
fontName ??= "Times New Roman"; // Assign only if fontName is nullCommon use: lazy initialization
Example:
This means LoadFromDatabase() is called and its result assigned only when _cache is null. If _cache already has a value, we return it directly. This is a common pattern for lazy initialization. The example assumes that multiple threads do not initialize the field concurrently, however. ??= does not synchronize concurrent calls. For shared caches, see Section 4.6.
Null-conditional operators (?. and ?[])
The null-conditional operators are ?. and ?[]. Use ?. to access a member (a property or method) only if the receiver isn’t null; otherwise, the entire expression evaluates to null. You use ?[] similarly for indexers.

Start with ?.. The following example calls obj.ToString() directly:
If the caller passes null for obj, you’ll get a NullReferenceException. Rewrite it like this:
Line 3 is equivalent to:
return (obj == null ? null : obj.ToString());With indexers: ?[]
To access an indexer safely, use ?[]:
This means: if str is null, return null; otherwise try to return the first character.
Important: ?[] only checks whether the receiver is null. It does not check whether the index is in range. So if str is an empty string "", str?[0] will still throw an IndexOutOfRangeException. If you want to treat both null and empty strings as “no character,” add a length check or use string.IsNullOrEmpty.
Chaining (short-circuit behavior)
Null-conditional chaining is also safe, for example:
There are two points to notice:
- If
objis null, the expression evaluates to null immediately, regardless of how many member accesses follow. This behavior is known as short-circuiting. - Here we also use
?.on the result ofToString(), because in projects with NRT enabled, if the receiver’s compile-time type isobject, the compiler treatsobject.ToString()as possibly returningnull. Writing it this way avoids unnecessary null warnings.
For multi-level member access, ?. lets you chain the null checks through each level. For example:
int? orderCount = customer?.Orders?.Count;Even if customer or Orders is null, the program does not throw; it returns null.
Without ?., you have to expand each null check:
Reminder: in these examples, the variables that receive the result are nullable (int?, char?). That’s required because the result can be null.
Combining operators
Null-coalescing and null-conditional operators are often used together:
If the caller passes null, this method returns "N/A".
Note that the return type is string, not string?. The pattern of “accepting nullable inputs but guaranteeing a non-null result” is very practical: callers can use the return value without extra null checks.
Note: The compiler can only distinguish
stringfromstring?and provide null-safety warnings when NRT is enabled. See Section 3.4.
Other common combinations:
When you need a fallback for multi-level nullable values, this style keeps safe access and defaulting in the same expression.
Null-conditional assignment operator (C# 14)
C# 14 extends ?. and ?[] so they can appear on the left-hand side of an assignment. This gives you a concise way to express “assign only if the receiver isn’t null.”
Previously, you had to do a null check first:
Now you can write:
// C# 14: null-conditional assignment
customer?.Order = GetCurrentOrder();This means: “Assign the result of GetCurrentOrder() to customer.Order only when customer is not null.”
Important detail: the expression on the right side is evaluated only if the receiver is not null. If customer is null, GetCurrentOrder() is not called.
This also works with compound assignments:
Note: C# 14 null-conditional assignment supports ordinary assignment and compound assignment operators such as += and -=, but the ++ and -- operators cannot be used with null-conditional access. This is a language restriction defined for the C# 14 syntax.
Source code: DemoNullOperators
3.4 Nullable reference types
As Section 3.1 showed, NRT enables compiler warnings about possible null references. Both int? and string? use a question mark, but the mechanisms differ: the former is Nullable<int>, while the latter is still a string reference, with a nullability annotation for compiler analysis. Let’s look at how to configure and use NRT.

Enabling the nullable context
For backward compatibility, older .NET projects often had NRT disabled by default. Starting with .NET 6, new project templates enable NRT by default, but whether it is enabled still depends on project settings. To enable or disable it explicitly, set the Nullable property in your .csproj:
You can also enable it for a single file with a preprocessor directive:
#nullable enableFile-level control is useful when migrating a legacy codebase gradually instead of fixing everything at once.
Core concepts: ? and ! annotations
With NRT enabled, type annotations express a variable’s nullability contract:
The ? explicitly tells the compiler “this variable may be null.” Without ?, the compiler treats it as “should not be null.”
Note
If NRT is not enabled,
stringstill behaves like it did in older C#: null is allowed by default. In that state, even if you writestring?, the compiler usually treats it as an annotation only, and you may even see warnings such as CS8632 telling you that nullable annotations are not currently enabled, rather than fully tracking and enforcing nullability under NRT rules. With NRT enabled,stringis treated as non-nullable, and you usestring?to explicitly represent “may be null.” This change encourages you to handle nulls intentionally.
Non-nullable reference types
For non-nullable declarations, the compiler treats them as safe to use under the current nullability contract and flow-analysis result:
Keep in mind that “safe” here means the compiler does not issue a nullability warning based on the information it currently has. It does not mean runtime nulls are impossible. If an external API, legacy code path, or third-party library violates its contract, you can still get a runtime failure.
Nullable reference types
If the compiler determines that a reference may be null, accessing its members directly produces a warning:
To eliminate the warning, check first:
Compiler flow analysis
The compiler performs static flow analysis to track the null state of variables. It recognizes common guard patterns:
The compiler also understands checks such as is null and short-circuit logic. For example:
After this check, the compiler treats the null state of input as not-null, without changing its declared type.
However, flow analysis has limits. It can’t “see through” code like this:
Flow analysis has an important limitation: without extra nullability contract information, it usually reasons only from the visible control flow and its built-in rules.
So even though ValidateName throws an exception on null, the compiler does not know that. When it checks the later name.Length access, it still cannot prove that name is non-null, so it reports a warning.
In cases like this, you can use the [NotNull] attribute to help the compiler understand:
[NotNull] means that if the method returns normally, the parameter should not be null. The compiler updates the caller’s null state based on this contract, so the earlier name.Length no longer produces a warning. The implementer must uphold that contract, as in the example that throws when n is null.
If a method initializes members of an object, use [MemberNotNull], discussed shortly. If a method returns a Boolean and a parameter is guaranteed non-null only on success, use [NotNullWhen(true)], covered in Section 3.6. All three convey nullability contracts to the compiler rather than adding runtime checks.

Null-forgiving operator (!)
Sometimes the compiler can’t prove a value is non-null, even when you know it’s safe. In such cases, you can use the ! operator to tell the compiler: “I guarantee this isn’t null—don’t warn me.”
Example:
The key point is that some legacy APIs or third-party libraries may expose a string? signature even though, under a specific contract or call path, you know the value is guaranteed to be non-null. That is the kind of situation where ! can be reasonable.
A word of caution: don’t use ! merely to silence warnings. Doing so pushes a risk the compiler could have flagged back into runtime behavior. Use ! only when you are genuinely certain.

Common scenarios where ! can be useful:
(1) Delayed initialization the compiler can’t track
While null! can be a reasonable stopgap in this kind of scenario, [MemberNotNull] is often a better way to tell the compiler that the field is guaranteed to be initialized here. For example, the previous sample can be rewritten like this:
using System.Diagnostics.CodeAnalysis;
public class MyClass
{
// Use an attribute instead of ! to describe the initialization
// guarantee
private string _value;
public MyClass()
{
Initialize();
}
public string Value => _value;
[MemberNotNull(nameof(_value))]
private void Initialize()
{
_value = "Initial value";
}
}Applying [MemberNotNull] to Initialize tells the compiler: if this method completes successfully, _value can be treated as initialized and no longer needs to be considered null.
Tip
Starting with C# 11, if a public property must be supplied by the caller during object creation, consider using the
requiredmodifier instead (see Chapter 4). However, this example shows a private field initialized inside the class’s construction flow, which is not the main use case forrequired.
(2) Framework-provided property injection (Blazor as an example)
Some frameworks support property injection, but the exact behavior depends on the framework. The example above uses a Blazor component: the framework sets the property value at runtime, but the compiler cannot infer that from the code alone, so = null!; is commonly used here.
In ordinary application services, constructor injection is still the preferred approach. In other words, you receive dependencies through the constructor, which usually avoids the need for this kind of null!.
(3) Test setup code
Note that a test framework mechanism such as [SetUp] usually guarantees only that the initialization method runs before the test. It does not guarantee that CreateTestSubject() itself returns a non-null value.
If CreateTestSubject is not supposed to return null in the first place, the better fix is usually to declare its return type as non-nullable. Using ! here makes sense mainly when you’re constrained by legacy code, test tooling, or an existing helper signature and you know that this particular call won’t return null.
Food for thought
Suppose a legacy API has the following signature and the caller wants to retrieve a required user name:
Compare these two situations:
- If the API documentation explicitly guarantees a non-null result under the conditions of this call, is using
!reasonable? - If the API may also return null when data is missing, should you keep
!or use an explicit null check or another way to handle the missing value?
Explain compiler warnings and runtime results separately, rather than simply suggesting how to silence warnings. Chapter 12 discusses a more advanced example involving asynchronous operations.
Real-world example: API design
When designing APIs, correct null annotations save callers from guessing. A quick glance at the method signature should tell callers what may be null:
#nullable enable
public class UserService
{
// Return value is non-null
public User GetUser(int id)
{
var user = FindUserInDatabase(id);
return user ?? throw new UserNotFoundException(id);
}
// Return value may be null
public User? FindUser(string email)
{
return FindUserByEmail(email); // Might not exist
}
// Parameters are non-null
public void UpdateName(User user, string name)
{
// Under the current nullable contract,
// the compiler can treat user and name as non-null
user.Name = name;
}
// Parameter may be null
public void UpdateNickname(User user, string? nickname)
{
user.Nickname = nickname; // nickname is allowed to be null
}
}Source code: DemoNullableReferenceType
Compatibility with legacy code
NRT itself is a compile-time check and does not automatically add runtime null checks. For existing code:
- Enabling NRT itself does not change the behavior of existing null accesses. Some libraries read nullability annotations and adjust their own behavior, however, so migration still requires testing.
- Even with NRT warnings, the project can still build and run (unless you treat warnings as errors, covered later).
- You can adopt NRT incrementally instead of fixing everything immediately.
Suggested approach:
- For new projects, use the default (project-level NRT enabled).
- For older projects, start by enabling
#nullable enablein individual files and migrate step by step.
Separating annotation and warning contexts (advanced)
#nullable enable actually does two things:
- Annotation context: Treat reference types as non-nullable unless they’re annotated with
?. - Warning context: Produce null-safety warnings.
You can control them separately. For example, enable only one of them in a file:
#nullable enable annotations // Enable annotations only (no warnings)Or:
// Enable warnings only (doesn't change annotations)
#nullable enable warningsYou can also enable both (equivalent to #nullable enable):
You can do the same at the project level:
Practical use: when migrating a large legacy codebase, you might start with annotation context only:
This gives downstream consumers clear nullability contracts right away, while you fix internal warnings gradually.
Treating null warnings as errors
For new projects, it’s often a good idea to promote null warnings to errors:
The example promotes only the three specified warnings to errors. To treat all nullability-related warnings as errors, use <WarningsAsErrors>nullable</WarningsAsErrors>.
Common null-related warning codes include:
- CS8600: Converting a null literal or possible null value to a non-nullable type
- CS8602: Dereference of a possibly null reference
- CS8603: Possible null reference return
AI-assisted development
Fix all CS86xx warnings in this .NET project. The project was upgraded from an older .NET version and has never had NRT enabled. Please first judge the actual intent of the code: only mark a type as nullable (
T?) whennullis truly a valid value. If a value should not be null, prefer fixing the initialization flow, adding guard clauses, applying appropriate nullability attributes, or refactoring the code to eliminate the warning. Use!only as a last resort when you have strong reasons to know the compiler cannot infer the guarantee.After each
.csfile change, pause. I will review, rebuild, and check whether the warning count decreases. Only continue when I tell you to.
3.5 Pattern matching and null
Pattern matching is a major C# feature; see Chapter 6 for a full discussion. This section focuses on using pattern matching for null checks.
is null and is not null (C# 9+)
Use is null and is not null to directly check whether a reference is null:
is null has been available since C# 7, and is not null since C# 9. Unlike == null or != null, these patterns check for null directly without calling custom equality operators, even when the type overloads them.
Switch expressions and null
When returning different results for several conditions, a switch expression can include null as one of its arms, without a separate if check. See constant patterns in Section 6.4.
Property patterns and null
A property pattern can check whether an object is null and whether its properties satisfy conditions. Section 6.6 compares traditional null checks, null-conditional operators, and property patterns using the same example.
Source code: DemoNullPatternMatching (Includes advanced patterns from Chapter 6; you can run it after reading that chapter.)
Food for thought: compare null-checking styles
Construct an example showing why obj == null and obj is null may yield different results when a reference type overloads ==. Identify the operator used in the example, then run it and compare the results.
3.6 Best practices
Null handling is about more than eliminating compiler warnings. First decide whether missing data is a normal result for the method or a violation of its preconditions.
1. Prefer explicit nullability in your API
As in the API design example in Section 3.4, if not finding a user is a normal outcome, User? FindUser(...) expresses that the result may be missing. If the method must obtain a user to proceed, use User GetUser(...) and explicitly handle failure, for example by throwing an appropriate exception.
2. Check early to prevent null from spreading
// ✘ Null spreads through the entire method
public void ProcessOrder(Order? order)
{
var items = order?.Items; // items may be null
var count = items?.Count; // count may be null
// ...now the whole method has to deal with null
}
// ✔ Guard clause / early return
public void ProcessOrder(Order? order)
{
if (order is null) return;
// Everything below can assume order is not null
var items = order.Items;
var count = items.Count;
}This guard-clause pattern keeps the method body concise and makes its preconditions clear. The example assumes doing nothing when there is no order is valid. If an order is required, explicitly reject null rather than silently returning.
3. Use nullability attributes (advanced)
Use nullability attributes for conditions that type annotations alone cannot express. Section 3.4 introduced contracts for normal returns and member initialization. Another common case is a Try method that guarantees a non-null value only when it returns true.
[NotNullWhen(true)] tells the compiler that when the method returns true, the out parameter can be used without another null check. The attribute does not add a runtime check. The implementer of TryGetUser must ensure user really is non-null when returning true.
4. Combine modern syntax thoughtfully
If a missing value has a meaningful fallback, combine ?. and ?? as in Section 3.3. For example, choose the profile’s display name first, then the user name, then a default string:
Not every missing value should be replaced with a default, though. A quiz score that has not been entered is not necessarily zero. Establish what the data means before choosing the syntax.
AI-assisted development: detect null risks
When maintaining legacy projects, use the AI prompt in Section 3.4 to address null warnings individually. In addition to rebuilding, test both present and missing values to verify the revised code still satisfies the original API contract. Fewer warnings do not mean all null risks have been eliminated.
Summary
- Nullable value types (
T?): Let value types represent “no value.” - Null operators (
??,??=,?.,?[]): Simplify null-handling logic and make code safer. C# 14 further extends this with null-conditional assignment. - Nullable Reference Types (C# 8+): When enabled, the compiler uses nullability annotations and flow analysis to warn about potential null-reference errors, without guaranteeing that runtime errors cannot occur.
- Pattern matching:
is nullandis not nullare unaffected by custom equality operators; Chapter 6 covers more complex patterns. - Best practices: Annotate explicitly, guard early, and use modern syntax to significantly reduce null risks; remember that NRT is a compile-time safeguard, so external inputs and legacy boundaries still require proper defenses.
In the next chapter, we’ll look at immutability and how it supports safer, more maintainable designs.
Glossary
| Term | Description |
|---|---|
| Guard clause | An early check at the start of a method to validate inputs and return/throw quickly. |
| Null | A special value representing “no object” / “no reference.” |
| Null-coalescing assignment operator | ??=; assigns only when the left-hand side is null. |
| Null-coalescing operator | ??; provides a fallback value when the left-hand side is null. |
| Null-conditional operator | ?. and ?[]; access members/indexers safely when the receiver might be null. |
| Null-forgiving operator | !; tells the compiler “treat this as non-null,” suppressing nullability warnings. |
| Nullable reference type (NRT) | C# 8+ feature that lets the compiler analyze and warn about possible null dereferences. |
| Nullable value type | A value type that can represent no value via T? (Nullable<T>). |
| Null propagation | A behavior where an expression becomes null when a null value participates in the computation. |
| NullReferenceException | The runtime exception thrown when dereferencing a null reference. |
| Pattern matching | C# syntax for matching shapes/conditions; useful for clear and safe null checks. |