C# and .NET • Modern C# baseline
Nullable Reference Types
Represent possible absence explicitly and respond to null before using a value.
Nullable reference type annotations let the compiler warn when code may dereference a missing reference. They are static analysis aids, not runtime validation: data from JSON, databases, and external APIs can still violate assumptions. Validate at boundaries and keep nullable annotations enabled in new projects.
Key points
- string and string? communicate different expectations to callers.
- The null-forgiving operator suppresses a warning; it does not make a null value safe.
Example
static string DisplayName(string? name)
{
return string.IsNullOrWhiteSpace(name) ? "Guest" : name.Trim();
}
Console.WriteLine(DisplayName(null));
What to expect
Guest
Practice exercise
Update the method to accept whitespace and a valid name, then test all three cases.
Key idea: Use nullable annotations to make uncertainty visible, and still validate values received at runtime.