Have you ever tried to use the Convert.ChangeType()
method to convert a value to a Nullable<T>
type? Awkwardly, it will throw an InvalidCastException
saying "Null object cannot be converted to a value type".
Try running this on your immediate window: ?System.Convert.ChangeType(null, typeof(int?))
For some obscure reason, Nullables are considered value types. For example, typeof(int?).IsValueType
returns true
.
For me, since Nullable<T>
accept null
, it's a class type, not a value type. Does anyone know why it would implemented differently?