Allocation of memory for an Array

All types are derived from the Object class, but the value types aren't allocated on the heap. Value type variables actually contain their values. so how then can these types be stored in arrays and used in methods that expect reference variables ? Can somebody please explain me how these value types are stored on heap when they are part of an array?


Boxing and Unboxing. Also see Here for info pertaining to arrays specifically (part way down). Note this is for object arrays, a valuetype array (eg int[] ) doesn't have any (un)boxing.


Have a look at this question:

Arrays, heap and stack and value types

You can pass the instance of a value type to a method expecting an object (ref class). In this case boxing and unboxing happens.

Value type arrays do not require boxing or unboxing!


The CLR handles arrays of value types specially. Of course an array is a reference type which is allocated on the heap, but the value type values are embedded into the heap record (not on the stack).

Similarly, when a reference type class contains a value type field, the value of the field is embedded into the record on the heap..

链接地址: http://www.djcxy.com/p/78890.html

上一篇: 内存范围查找+ 5M记录表

下一篇: 为阵列分配内存