Nullable Reference Types
Make possible null values visible to the compiler.
Introduction
Nullable annotations help prevent NullReferenceException by distinguishing optional references from required ones.
string? permits null. The null-coalescing operator supplies a safe fallback before Length is accessed.
Runnable C# example
Program.cs
using System;
class Program
{
static void Main()
{
string? nickname = null;
string displayName = nickname ?? "Anonymous";
Console.WriteLine($"{displayName} ({displayName.Length})");
}
}
Anonymous (9)Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.