Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

C# nullable value types: int? and Nullable<T>

Reference for C# nullable value types (int?, Nullable<T>): HasValue, Value, GetValueOrDefault, lifted operators, ?? and ?., boxing and pattern matching.

A value type such as int, bool or DateTime always holds a value. Appending ? to the type name, as in int?, makes a nullable value type: it can hold any int or the special value null. It is shorthand for the generic struct System.Nullable<T>, where T must itself be a non-nullable value type.

Nullable value types model "unknown" or "not provided": an optional age, a database column that allows NULL, a missing measurement. The compiler lifts the usual operators to work with them, so a + b is null when either side is null, and comparisons with null follow SQL-like rules.

C#
int? age = null;
int years = age ?? 0;

Try it

C# HasValue, Value and ??

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Boxing, parsing and the lifted bool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
int? x = null;
Nullable<int> y = 3;            // same type
int z = x ?? 0;                 // null-coalescing
int w = (int)y;                 // explicit unwrap, throws on null
Member Description
HasValue true when a value is present.
Value The underlying value; throws InvalidOperationException if there is none.
GetValueOrDefault() / GetValueOrDefault(d) The value, or default(T) / d when null.
Nullable.GetUnderlyingType(Type) The T of a Nullable<T> type, or null.
Nullable.Equals, Nullable.Compare<T> Null-aware comparison helpers.

Conversions. T converts implicitly to T?; T? converts to T only explicitly, which throws if the value is null. null converts to every nullable type.

Errors / undefined behavior. Reading .Value or casting null to T throws InvalidOperationException. Unboxing a null reference to T throws NullReferenceException, but unboxing to T? gives a null T?.

Usage notes

  • Lifted operators. For + - * / < > <= >=, a null operand makes arithmetic return null and comparisons return false. == and != treat two nulls as equal.
  • bool? has three-valued logic, where null & false is false and null | true is true. See bool.
  • Boxing. A T? with a value boxes as a plain boxed T; a T? without a value boxes as null. GetType() on a nullable variable never reports Nullable<T>, because the variable is boxed first.
  • Size. int? takes 8 bytes (4 for the value, 1 for the flag, plus padding), so large arrays of nullable values cost more memory.
  • Prefer pattern matching. if (x is int v) and if (x is null) are clearer than reading HasValue/Value pairs.
  • Do not confuse with nullable reference types: string? is only an annotation for the compiler, whereas int? is a different runtime type.
  • Database and JSON. ORMs and serializers map null columns and null JSON values to nullable value types; non-nullable properties receive an error or a default.
  • Constraints. where T : struct accepts int but not int?.