Adopting Nullable Reference Types in .NET 8: From Runtime Crashes to Compile-Time Safety
Enabling nullable reference types in .NET 8 turns runtime NullReferenceExceptions into compile-time warnings. Here's how to adopt it incrementally in a legacy codebase with a concrete example and the trade-offs to watch.
07 Feb 2026, 06:01 UTC

The problem: runtime surprises in legacy code
If you've maintained a .NET 7 codebase, you know the drill: a NullReferenceException surfaces in production, the stack trace points to a line that looks safe, and you spend hours tracing why an object was null. The compiler didn't warn you because, by default, every reference type is treated as non-nullable—even when the code clearly allows nulls.
Nullable reference types (NRT) flip that default. When enabled, the compiler tracks nullability through your code and emits warnings (CS8600–CS8604) wherever a null could be dereferenced. The feature has been around since C# 8.0, but .NET 8 makes it the default for new projects and sharpens the analysis. Adopting it in an existing codebase turns a class of runtime crashes into build-time feedback—without changing runtime behavior.
Enable the feature without breaking the build
Start by opting in at the project level. Edit your .csproj and add:
<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>
This switches the project-wide context to nullable annotations enabled and nullable warnings enabled. If you prefer a gradual rollout, you can instead place #nullable enable at the top of individual files. The project-level setting is simpler for new work; file-level directives help when you're migrating a large solution piece by piece.
After the change, run dotnet build. You'll likely see a wave of warnings. That's expected—the compiler is now doing its job.
Read the diagnostics, then decide how to fix
The most common warnings are:
- CS8600 – Converting null literal or possible null value to non-nullable type.
- CS8601 – Possible null reference assignment.
- CS8602 – Dereference of a possibly null reference.
- CS8603 – Possible null reference return.
- CS8604 – Possible null reference argument.
Each warning points to a place where the static analysis thinks a null could flow into a non-nullable context. You have three practical responses:
- Fix the root cause – Add a null check, use
ArgumentNullException.ThrowIfNull, or provide a default value. - Annotate the type as nullable – Change
stringtostring?when null is a valid, expected state. - Suppress locally – Use
#nullable disablearound a tricky block or the null-forgiving operator!when you're certain the value isn't null (e.g., after a guard clause). Use sparingly; overuse defeats the purpose.
Worked example: a customer lookup method
Consider this minimal API endpoint in a .NET 8 console project targeting net8.0:
// Customer.cs
public class Customer
{
public string Name { get; set; } = "";
public string? Email { get; set; }
}
// Program.cs
#nullable enable
using System;
string? GetName(Customer? cust) => cust?.Name;
var customer = new Customer { Name = "Ada" };
Console.WriteLine(GetName(customer));
Console.WriteLine(GetName(null));
With <Nullable>enable in the project file, dotnet build produces:
Program.cs(6,36): warning CS8600: Converting null literal or possible null value to non-nullable type.
The warning fires on cust?.Name because cust is Customer? and Name is non-nullable string. The expression cust?.Name can be null when cust is null, but the method returns string?—so the assignment is actually fine. The compiler is being cautious because Name itself is non-nullable.
Two clean fixes:
- Make the return type match the reality – Keep
string?and document that null means "no customer". The warning disappears because the return type already allows null. - Guard the input – If the method should never receive null, enforce it:
string GetName(Customer cust)
{
ArgumentNullException.ThrowIfNull(cust);
return cust.Name; // non-nullable return, no warning
}
The second approach shifts the contract: callers must provide a non-null Customer, and the method guarantees a non-null string. Rebuild and the warning vanishes.
Trade-offs and limitations
- Requires .NET 8 SDK (or at least the C# 12 compiler). Older projects must upgrade or rely on the legacy analyzer, which misses some newer warnings.
- Compile-time cost – Heavy annotation usage can increase build time and memory consumption during analysis. For most solutions the impact is negligible, but very large codebases may notice a difference.
- False positives – Developers unfamiliar with the syntax sometimes add
!or#nullable disableto silence warnings without understanding the flow, re-introducing risk. - Mixed contexts – If you enable NRT in one assembly but reference a library without annotations, the compiler treats all its reference types as oblivious (effectively
T?), which can produce noisy warnings at the boundary.
Next steps: adopt incrementally
- Create a new branch, add
<Nullable>enableto one project, and rundotnet build. - Fix the highest-impact warnings first—public APIs, data-access layers, and any code that has caused production null-reference crashes.
- Treat warnings as errors (
TreatWarningsAsErrorsorWarningsAsErrorsin the project file) once the critical paths are clean. - Enable the feature project-by-project until the whole solution compiles cleanly.
The payoff is a codebase where "object reference not set to an instance of an object" becomes a compile-time error you fix before merging—not a 3 a.m. page.