Null-Forgiving Operator

noun

say it: “NUL fer-GIV-ing OP-er-ay-ter” /nʌl fərˈɡɪvɪŋ ˈɒpəreɪtər/

A ! after a value that tells the C# compiler you are sure it is not null.

What is Null-Forgiving Operator in C#?

Null-Forgiving Operator — A ! after a value that tells the compiler "I know this isn't null", which silences its warning. It changes nothing when the program runs, so if you're wrong you still get a NullReferenceException.

string? title = Console.ReadLine();   // the compiler warns: may be null
int length = title!.Length;           // you promise it isn't

In a sentence

  • “I added the null-forgiving operator after ReadLine() because the input here is never empty.”

Level: Journeyman · More C# terms at this level

Updated