Nullable Types

Null Basics

Reference types can represent a nonexistent value with a null reference. Value types, however, cannot ordinarily represent null values. For example:

	string s = null;       // OK, Reference Type
	int i = null;          // Compile Error, Value Type
	                       // cannot be null

To represent null in a value type, you must use a special construct called a nullable type. A nullable type is denoted with a value type followed by the ? symbol:

	int? i = null;                    // OK, Nullable Type
	Console.WriteLine (i == null);    // True

Nullable<T> struct

T? translates into System.Nullable<T>. Nullable<T> is a light-weight immutable struct, having only two fields to represent Value and HasValue. The essence of System.Nullable<T> is very simple:

	public struct Nullable<T> where T : struct
	{
	  public T Value {get;}
	  public bool HasValue {get;}
	  public T GetValueOrDefault();
	  public T GetValueOrDefault(T defaultValue);
	...
	}

The code:

	int? i = null;
	Console.WriteLine (i == null);              // true

gets translated by the compiler to:

	Nullable<int> i = new Nullable<int>();
	Console.WriteLine (! i.HasValue);           // true

Attempting to retrieve Value when HasValue is false throws an InvalidOperationException. GetValueOrDefault() returns Value if HasValue is true; otherwise, it returns new T() or a specified a custom default value.

The default value of T? is null.

Implicit and explicit nullable conversions

The conversion from T to T? is implicit, and from T? to T is explicit. For example:

	int? x = 5;        // implicit
	int y = (int)x;    // explicit

The explicit ...

Get C# 3.0 Pocket Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.