Explicit nullable conversions
Explicit nullable conversions permit predefined explicit conversions that operate on non-nullable value types to also be used with nullable forms of those types. For each of the predefined explicit conversions that convert from a non-nullable value type S to a non-nullable value type T (§6.1.1, §6.1.2, §6.1.3, §6.2.1, and §6.2.2), the following nullable conversions exist: · An explicit conversion from S? to T?. · An explicit conversion from S to T?. · An explicit conversion from S? to T. Evaluation of a nullable conversion based on an underlying conversion from S to T proceeds as follows: · If the nullable conversion is from S? to T?: o If the source value is null (HasValue property is false), the result is the null value of type T?. o Otherwise, the conversion is evaluated as an unwrapping from S? to S, followed by the underlying conversion from S to T, followed by a wrapping from T to T?. · If the nullable conversion is from S to T?, the conversion is evaluated as the underlying conversion from S to T followed by a wrapping from T to T?. · If the nullable conversion is from S? to T, the conversion is evaluated as an unwrapping from S? to S followed by the underlying conversion from S to T. Note that an attempt to unwrap a nullable value will throw an exception if the value is null.
|