Nullable annotation context
Context: The nullable annotation context is controlled by #nullable directives. It enables or disables nullable warnings and annotations.
Directives
Section titled “Directives”#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 warningsProject‑level setting
Section titled “Project‑level setting”In .csproj:
<PropertyGroup> <Nullable>enable</Nullable></PropertyGroup>Real-world usage example
Section titled “Real-world usage example”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.