How to Use the Null-Forgiving Operator in C#
FindCapital("Japan")! tells the compiler: this can't be null here. The ! after the call removes the nullable warning, and changes nothing when the program runs.
Microsoft's reference: The unary postfix ! operator is the null-forgiving, or null-suppression, operator. In an enabled nullable annotation context, use the null-forgiving operator to suppress all nullable warnings for the preceding expression. A new console project has <Nullable>enable</Nullable> in its project file, so a method that returns string? makes the compiler warn wherever you treat its result as never null.
Syntax
<type> <name> = <expression that may be null>!;
| Part | What it is |
|---|---|
<expression>! | The expression with ! straight after it: the compiler treats it as not null |
The reference: The null-forgiving operator has no effect at run time. It only affects the compiler's static flow analysis by changing the null state of the expression.
The Example
A console app called NullForgiving has a FindCapital function that returns null for a country it doesn't know. The program looks up Japan, which is always in its list, and uses ! to say so.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n NullForgiving
cd NullForgiving
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/NullForgiving/NullForgiving.csproj:
Determining projects to restore...
Restored /Users/you/source/NullForgiving/NullForgiving.csproj (in 46 ms).
Restore succeeded.
dotnet new console -n NullForgiving creates a folder named NullForgiving with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or NullForgiving.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the list of capitals:
Dictionary<string, string> capitals = new()
{
["Japan"] = "Tokyo",
["Norway"] = "Oslo",
};
Then the lookup and the function:
// FindCapital can return null, but Japan is always in the list.
string capital = FindCapital("Japan")!;
Console.WriteLine(capital.ToUpperInvariant());
string? FindCapital(string country) =>
capitals.TryGetValue(country, out string? city) ? city : null;
string?says thatFindCapitalcan returnnull.FindCapital("Japan")!puts the result in astring, not astring?, without a warning.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
TOKYO

Japan was in the list, so the value wasn't null and ToUpperInvariant worked.
The Complete File

Dictionary<string, string> capitals = new()
{
["Japan"] = "Tokyo",
["Norway"] = "Oslo",
};
// FindCapital can return null, but Japan is always in the list.
string capital = FindCapital("Japan")!;
Console.WriteLine(capital.ToUpperInvariant());
string? FindCapital(string country) =>
capitals.TryGetValue(country, out string? city) ? city : null;
The Build With and Without the !
Build the project:
dotnet build --no-incremental
--no-incremental makes the build compile again even though dotnet run has just built the project.
NullForgiving -> /Users/you/source/NullForgiving/bin/Debug/net10.0/NullForgiving.dll
Build succeeded.
0 Warning(s)
0 Error(s)
No warnings. The same build with the ! taken out:
/Users/you/source/NullForgiving/Program.cs(8,18): warning CS8600: Converting null literal or possible null value to non-nullable type. [/Users/you/source/NullForgiving/NullForgiving.csproj]
/Users/you/source/NullForgiving/Program.cs(9,19): warning CS8602: Dereference of a possibly null reference. [/Users/you/source/NullForgiving/NullForgiving.csproj]
NullForgiving -> /Users/you/source/NullForgiving/bin/Debug/net10.0/NullForgiving.dll
Build succeeded.
/Users/you/source/NullForgiving/Program.cs(8,18): warning CS8600: Converting null literal or possible null value to non-nullable type. [/Users/you/source/NullForgiving/NullForgiving.csproj]
/Users/you/source/NullForgiving/Program.cs(9,19): warning CS8602: Dereference of a possibly null reference. [/Users/you/source/NullForgiving/NullForgiving.csproj]
2 Warning(s)
0 Error(s)
Line 8, where the result goes into a string, gets warning CS8600, and line 9, which calls a method on it, gets warning CS8602. The build lists each warning twice: once as it compiles and once in the summary.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using ! on a value that can really be null | It hides the warning, not the problem: FindCapital("Peru")! stored null, and ToUpperInvariant() then threw NullReferenceException: Object reference not set to an instance of an object. | Check for null instead, such as with is null, wherever the value can be missing |
FAQ
Is the ! checked when the program runs?
No. The reference: At run time, expression x! evaluates to the result of the underlying expression x. That's why the Peru lookup above let null through.
Conclusion
Put ! after an expression when you know it isn't null but the compiler can't tell. It only silences the warnings, so use it where a value truly can't be missing, and check for null everywhere else.
Additional Resources
- The null-forgiving operator — the operator.
- Nullable reference types — the warnings it silences.
