#1,060 – Explicit Conversions between Nullable Types
March 25, 2014 1 Comment
An implicit conversion exists between two nullable types if an implicit conversion exists between the corresponding value types. For example:
int i = 12; long l = i; // implicit int to long int? i2 = 12; long? l2 = i2; // implicit int? to long?
Similarly, an explicit conversion exists between two nullable types if an explicit conversion exists between the corresponding value types.
long l = 12; int i = (int)l; // explicit long to int long? l2 = 12; int? i2 = (int?)l2; // explicit long? to int?
You can also convert between a nullable and non-nullable type, either implicitly or explicitly. An explicit conversion is required when converting from a nullable type to a non-nullable type or when an explicit conversion is required by the underlying types.
int? i3 = (int?)l; // explicit long to int? long l3 = (long)i3; // explicit int? to long
Pingback: Dew Drop – March 25, 2014 (#1750) | Morning Dew