Nullable reference types

C# 8.0 NETCore 3.0

Published Updated Author Jeffrey T. Fritz Reading time

Nullable reference types make nullability part of the type system, which helps the compiler catch bugs before they reach production.

Nullable reference types were introduced in C# 8.0. They add static analysis for reference nullability so you can distinguish between required and optional values.

Why it matters

This feature targets one of the most common sources of .NET bugs: unexpected null references. Once annotations are enabled, the compiler warns when code might dereference a missing value.

That makes APIs easier to reason about and helps document intent directly in signatures.

Cautions

Nullable annotations do not change runtime behavior. They only improve compile-time analysis, so older APIs may still return null even when the type system says otherwise.

Treat warnings as design feedback. If you silence too many of them, you lose most of the safety benefit.

Marking a reference as optional

Nullable annotations force you to model absence explicitly instead of assuming every reference is populated.

Valid since C# 8.0

string? FindDisplayName() => null;

string? displayName = FindDisplayName();

if (displayName is null)
{
    return;
}

Console.WriteLine(displayName.Length);

Learn more

Nullable reference types (Microsoft Learn)