Nullable Types

You can make Nullable Types such as int and double to allow null to be their values. Null values are only storable to reference types like string and other objects. C# allows you to transform value types so that they can handle null values. You can use the System.Nullable<T> type where T is the type that will be transformed into a nullable type.

Nullable<int> nullInt = null; 
Nullable<double> nullDouble = null;

Alternatively, you can append the ? to the type.

int? nullInt = null;
double? nullDouble = null;

You can use the following code to test if a variable contains a null value.

if (nullInt == null)

if (nullDouble.HasValue)

Since we turned them to nullable types, they can’t be stored into a variable with an original non-nullable type. The following is not allowed.

int? nullInt = null;
int myNumber = nullInt;

You need to convert it back first to its original type.

int myNumber = (int)nullInt;

If you convert a nullable type with a null value back to its original form, then an exception will be thrown. When involving two nullable types(except bool?) in an operation, if any of the operands is null, then the result will be also null. For bool?, the following table shows the possible results. (Note that & and | also represents && and ||).

var1 var2 var1 & var2 var1 | var2
true null null true
false null false null
null true null true
null false false null
null null null null

If you want to prevent an expression from resulting into a null even if one of its operands can have a value of null, you can use the ??operator which is called the null coalescing operator.

int? nullInt = null;
int number = nullInt * 5 ?? 10;

The second line demonstrates the use of the ?? operator. To further illustrate the meaning of that operator, the below code which uses the ternary operator is equivalent to it.

int number = (nullInt * 5) != null ? (nullInt * 5) : 10;

If the expression of the left operand of the ?? operator results to null, then the operand at the right of the ?? operator will be picked and stored inside the variable. If the left operand does not result into a null, then the result or the value of the left operand will be stored instead.