Skip to content

Nullable annotation context

Context: The nullable annotation context is controlled by #nullable directives. It enables or disables nullable warnings and annotations.

#nullable enable // Enables nullable annotations and warnings
#nullable disable // Disables (default for legacy code)
#nullable restore // Restores to project default
#nullable enable annotations // Only annotations
#nullable enable warnings // Only warnings

In .csproj:

<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>

Migrating legacy code: Use #nullable disable at the top of old files to temporarily disable warnings while you gradually enable nullable contexts.

Example: The .NET runtime itself migrated to nullable reference types file by file using this technique.