CKTIS / C#
Nullable reference types and the #nullable context
The string and string? annotations express intent about null, while compiler flow analysis warns about unsafe use. The #nullable context controls annotations and warnings independently; it does not add runtime checks.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
T value;
T? optional;
#nullable enable
#nullable disable
#nullable restore
#nullable enable annotations
#nullable disable warnings
#nullable restore warningsNullable reference types were introduced in C# 8.0; they do not require preview mode in a .NET 10 project. With annotations enabled, string means a reference intended not to be null, while string? explicitly permits null. These annotate the same runtime type; they are not equivalent to Nullable<T> for value types such as int?.
The compiler distinguishes a value's declared annotation from its null-state in flow analysis. At method entry, the compiler ordinarily treats a string parameter as not-null and a string? parameter as maybe-null based on their annotations. The state can change as flow analysis proceeds. The compiler warns about assigning a possible null to a non-nullable reference and dereferencing a possibly null reference, for example through text.Length. After checking text is null and leaving that branch, analysis recognizes that subsequent use of text is safe. The text! operator only changes the compiler's assessment: it neither checks the value nor prevents NullReferenceException.
The project setting <Nullable>enable</Nullable> enables annotations and warnings. disable turns both off, annotations enables annotations only, and warnings enables warnings only. With annotations disabled, references without ? are nullable-oblivious, and using ? can produce a warning.
The directives #nullable enable and #nullable disable override settings for subsequent lines in the file. #nullable restore restores project settings, not the preceding directive. An optional target, annotations or warnings, such as #nullable disable warnings, changes only that flag. The effect lasts until another applicable directive or the end of the file.
This is compile-time assistance, not a guarantee against runtime null. For example, new string[3] initially contains null elements despite its string[] type. Fix data flow and check values instead of blindly suppressing warnings with !.
Reference identifiers covered by this entry: Nullable reference annotations and #nullable context.
Examples
#nullable enable
using System;
internal static class Program
{
private static void Main()
{
PrintLength(null);
PrintLength("Ada");
}
private static void PrintLength(string? text)
{
if (text is null)
{
Console.WriteLine("missing");
return;
}
Console.WriteLine(text.Length);
}
}
#nullable restoreResult
missing 3
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.