I recommend ??= all the time. It is one of my favorite C# operators for handling null.
So I was surprised when I turned on nullable reference types in an old project of mine, and the compiler flagged a line that used exactly that operator.
In this post, I will show you what one line in a .csproj found in a real .NET 5 project, why ??= was not enough, and how to turn nullable on in your own codebase without drowning in warnings.
TL;DR: I enabled nullable reference types in an old .NET 5 project with one line in the .csproj . The compiler found 14 null risks without running a single line of code. The most surprising one used ??= , an operator I usually recommend for null safety. Null-safe operators make your code shorter. The compiler is what makes it safe.
The Experiment
The project is a small Heroes API I built on .NET 5: heroes, their achievements, and a CQRS setup with MediatR. Nothing exotic. The kind of code most of us have somewhere.
I have been writing C# since 2016.…