1. Introduction
Why do you have to enable NRT separately?
Nullable Reference Types (NRT) aren't just syntactic sugar—they're a serious compiler helper in the fight against null-related bugs. But for historical reasons, this mode isn't enabled by default in all old projects—to avoid breaking compatibility. If you're creating a new project in the latest versions of .NET (like .NET 7, 8, or 9), NRT is probably already on by default. But if your project is kinda old or you want to manage this mode manually, you gotta do it explicitly.
What does Nullable Reference Types mode actually do?
When this mode is active, the compiler starts analyzing which reference variables can be null and which can't. It doesn't mess with your program's execution, but it'll throw a bunch of warnings if it spots a possible issue. It's a great way to write reliable, clear, and robust code.
How do you check if NRT mode is enabled?
If you have a new .NET 6 or higher project, NRT is probably already on. But you never know. Let's check!
- Open your project file. Usually, that's the file with the .csproj extension.
- Find or add the <Nullable> setting inside the first <PropertyGroup> tag.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
If you see <Nullable>disable</Nullable> there, it means NRT checks are off.
2. How to Enable NRT Mode
Through .csproj — for the whole project
- Open (or create) your .csproj file.
- Inside the <PropertyGroup> block, add or change this line:
<Nullable>enable</Nullable>
Now the compiler will keep a close eye on all .cs files in your project and yell at you for potentially dangerous null stuff.
Why do it this way?
Because it's a global setting. From now on, your whole project follows the new rules: null is suspicious, and sloppiness won't fly anymore!
Only for specific files or code chunks
If you're not ready to go strict for the whole project, you can practice on individual files. For that, use preprocessor directives:
#nullable enable
string? value = null; // Analysis is on here
#nullable disable
string legacyValue = null; // And here — it's off
#nullable restore
- #nullable enable — turns on checking till the end of the file.
- #nullable disable — turns it off.
- #nullable restore — brings back settings from .csproj or an external file.
It's handy: you can enable strict checks in new modules without touching old, unstable code.
What else can you set in <Nullable>
| Value | Behavior |
|---|---|
|
Turns on everything: annotations and warnings |
|
Completely turns off null-reference analysis |
|
Only warnings, no required annotations |
|
Annotations are needed, but the compiler won't complain |
|
Restores behavior to the original (like from a global setting) |
Usually, people use enable or disable.
3. Demo Example
Before NRT is enabled
// Old mode, NRT is off
string s = null; // No complaints
Console.WriteLine(s.Length); // Danger! Possible NullReferenceException
After NRT is enabled
// New mode, NRT is on
string s = null; // Warning: possible null assignment
Console.WriteLine(s.Length); // Warning: possible null reference access
Now your IDE will start underlining potential problems and the compiler will warn you: Dereference of a possibly null reference. (CS8602)
How do you fix this?
string? s = null;
if (s != null)
{
Console.WriteLine(s.Length);
}
This way, you're clearly saying: this variable can be null, and the compiler is happy.
4. What if your project is "old-school"?
Lots of projects were written back in the day when null was in pretty much every reference variable. Turning on NRT is like hiring a strict auditor who'll tell you about every possible problem.
What if there are too many warnings?
Don't freak out—these are not errors, just hints. Your program still compiles.
- You can enable NRT only in new files.
- Gradually clean up your code: add ?, check for null, use ! (more on that later).
- Remember: the more careful you are with null, the fewer problems you'll have later.
Backward compatibility and third-party libraries
- Enabling NRT doesn't change your code's runtime behavior, it just adds analysis at compile time.
- Old code might instantly throw a bunch of warnings.
- If you're using libraries without NRT annotations, the compiler will play it safe and assume everything could be null.
GO TO FULL VERSION