When the runtime allocates storage for the type or generates code to work on one, it checks to see if the type is from System.ValueType , but not System.ValueType . If the storage location does not meet these criteria, it will contain a link to the heap object, and any code for working with its members (fields, methods, properties, etc.) will act on the specified object. Otherwise, the storage will store all public and private fields of this type (which will be specified identically in all places of storage of this type), and any code for working with its members will work at the storage location itself.
If an attempt is made to save a value type in the storage of the ValueType class or a storage location that cannot be obtained from ValueType , the system will generate a new heap object of the type of the storage location, and then save the link to this object in the appropriate storage location. Although the storage locations for the types obtained from System.ValueType and the code for accessing them are handled specifically at runtime, instances of heap objects that inherit from System.ValueType (for example, just created, just mentioned) are simply heap objects that come from System.ValueType , and have inheritance behaviors that are essentially the same as other types, and therefore can be passed in code that awaits processing of heap references.
An operator of type Object Foo = New System.Drawing.Point(3,4); really includes three kinds of things:
- An unknown temporary storage of the type `System.Drawing.Point (3,4)`, which contains private fields of this type (two integers) and is initialized (3,4). Note that this object does not store `Object`; it contains two integers that the compiler knows, represent their fields.
- A heap object of type `System.Drawing.Point`. Since this is a heap object, it inherits from `System.Object`, like all heap objects.
- A storage location of type `Object`, which is represented by the variable name` Foo`.
The result of all this is that while value types can be defined as inherited from ValueType , which inherit from Object , while heap objects whose types inherit from ValueType , inherit from Object , storage types do not contain value types objects that inherit from Object .
supercat
source share