Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Nullable: practical reference

System.Nullable: practical reference: Nullable<T> represents either a struct value or absence through HasValue=false.

Version: C# built-in type reference; .NET 10 runtime

Syntax

T? value / System.Nullable<T> value

Nullable represents either a struct value or absence through HasValue=false.

Use it for optional value types and mapping null from data sources.

Accessing Value when absent throws InvalidOperationException; GetValueOrDefault can hide absence.

Boxing a present nullable boxes T, while an absent nullable boxes to null.

The complete program actually uses System.Nullable; its exact expected stdout is False:0 True:7.

Examples

using System;

Nullable<int> value = null;
Console.WriteLine($"{value.HasValue}:{value.GetValueOrDefault()}");
value = 7;
Console.WriteLine($"{value.HasValue}:{value.Value}");

Result

False:0
True:7

The complete program actually uses `System.Nullable`; its exact expected stdout is `False:0 True:7`.

Sources