Despite the fact that C # pretends that value type storages store instances of types obtained from System.ValueType , which, in turn, come from System.Object , this is not so. Each type derived from System.ValueType actually represents two very different types of things:
- A set of bytes that (for primitive types) represents data directly, or (for non-primitive structure types) contains the contents of all fields, public and private, but does not contain information about the type.
- A separate heap object that contains the object header in addition to the above, whose type is derived from `System.ValueType`.
A value type storage location holds the first; heaps of objects of type value hold the second.
For various reasons, Microsoft decided that Nullable<T> should only support first use. If you try to pass a storage location of type Nullable<T> to code that expects a reference to the heap object, the system converts the element to T if HasValue is true, or just pass a null reference if HasValue is false. Although there are ways to create a heap object of type Nullable<T> , the usual methods of converting a store of values ββof a type to a heap object will never generate one.
Please also note that calling GetType() in the storage location of the value will not actually determine the type of storage location, but instead converts the contents of this storage location to a heap object and then returns the type of the resulting object, since storage locations of type Nullable<T> converted either to instances of the object T , or to zero, nothing in the instance of the object will tell if the storage location from which it was was Nullable<T> .
supercat Apr 2 2018-12-12T00: 00Z
source share