Advertisement
Beginner C#, Nullable Reference Types

What is the null-forgiving operator (!) and when is it appropriate?

Short answer

! tells the compiler you know a value isn't null, suppressing the warning. Use it sparingly when you have knowledge the compiler lacks — overuse hides real bugs.

Save C# mock interview
Advertisement
AModel answer
! tells the compiler you know a value isn't null, suppressing the warning. Use it sparingly when you have knowledge the compiler lacks — overuse hides real bugs.
var name = dict[key]!; // you guarantee the key exists
Real-world example
Asserting non-null after a framework guarantees initialization the compiler can't see.