#566 – Implicit Conversions to Nullable Types
April 20, 2012 2 Comments
A nullable type represents a type whose value can be either a particular value type or can be the null value.
int i = 12; // regular int, can't be null int? j = 22; // Nullable int, can store an int value j = null; // Can also store null value
You can implicitly convert from the corresponding value type to its matching nullable type. For example, you can convert from an object of type int to an object of type int?
int i = 12; int? j = i; // Implicit conversion from int to int? float f1 = i; // Implicit conversion from int to float float? f2 = i; // Implicit conversion from int -> float -> float?
You can also implicitly convert from the null value to any nullable type.
int? i = null;