Skip to content

Nullable reference types C# 8 and later

Context: Nullable reference types help prevent null reference exceptions by distinguishing between nullable and non‑nullable reference types.

#nullable enable
string nonNullable = "Hello";
string? nullable = null;
nonNullable = nullable; // warning: possible null assignment

API contracts: Mark optional fields as string? and required fields as string. This documents the contract and the compiler enforces null checks.

Example: In ASP.NET Core, model binding respects nullable annotations. The Swagger/OpenAPI generator uses them to mark optional parameters.