Skip to content

EN CSharp Nullability

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

null, nullable types and checks

Manual C# · Examples UO

T? for a value type allows no value, for example int?. Nullable annotations on reference types help the compiler find potential references to null; they do not add automatic runtime checking. ?. performs conditional access, ?? specifies replacement, ??= assigns under null.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
string? name = null;
System.Console.WriteLine(name ?? "Unknown");

Result:

Unknown
string? name = null;
string display = name ?? "unknown";
int length = name?.Length ?? 0;
UO.Print($"{display}: {length}");
int? amount = null;
amount ??= 1;
if (amount is int value)
{
    UO.Print(value.ToString());
}

The ! operator suppresses the null parse warning, but does not prevent the exception. For a nullable value type, check HasValue, use a wildcard, or GetValueOrDefault. A missing object and a numeric zero are different states.

Official documentation: null, nullable types and validations - Microsoft Learn.

Clone this wiki locally