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.
int? age = null; int years = age ?? 0;
Try it
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.
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
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
+ - * / < > <= >=, anulloperand makes arithmetic returnnulland comparisons returnfalse.==and!=treat two nulls as equal. bool?has three-valued logic, wherenull & falseisfalseandnull | trueistrue. Seebool.- Boxing. A
T?with a value boxes as a plain boxedT; aT?without a value boxes asnull.GetType()on a nullable variable never reportsNullable<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)andif (x is null)are clearer than readingHasValue/Valuepairs. - Do not confuse with nullable reference types:
string?is only an annotation for the compiler, whereasint?is a different runtime type. - Database and JSON. ORMs and serializers map
nullcolumns andnullJSON values to nullable value types; non-nullable properties receive an error or a default. - Constraints.
where T : structacceptsintbut notint?.