The definition of each type derived from System.ValueType , with the exception of System.Enum , actually defines two types of things: the type of the heap object and the type of storage location. Instances of the latter can be implicitly converted to the former (creating a copy of the data contained in it), and instances of the former can be explicitly attributed to the latter (similarly); although both types are described by the same System.Type , and although they have the same elements, they behave differently.
A List<AnyClassType> expects to contain a bunch of heap references; whether the list in question is List<String> , List<StringBuilder> , List<Button> or something else, may be of interest to users of the list, but not of interest to the List<T> . If one of the List<Button> sends to IEnumerable<Control> , the one who calls its GetEnumerator() method expects to get an object that will display heap objects that come from Control ; returning from List<Button>.GetEnumerator() will satisfy this expectation. In contrast, if someone were to drop List<Int32> to List<Object> , then who called GetEnumerator() would expect something that displays heap object references, but List<Integer>.GetEnumerator instead it will give something that outputs integers of type values.
You can store Int32 values ββin a List<Object> or List<ValueType> ; storing an integer in such a list converts it into its heap object form and saves a link to it; calling GetEnumerator() will give something that displays heap links. However, there is no way to indicate that such a list will only contain heap type instances matching Int32 . In C ++ / CLI, it is possible to declare variables of the type "reference to the stored heap evaluation method", but the mechanisms underlying common types in .net cannot work with such types.
supercat
source share