Generics memory management

后端 未结 7 714
慢半拍i
慢半拍i 2021-01-05 02:41

I have question regarding how memory is managed for strong type Generics

List ints1 = new List();
ints1.Add(1); ints1.Add(2); ints1.Add         


        
7条回答
  •  迷失自我
    2021-01-05 03:02

    The "new" syntax is used to initialize both value-types and reference-types. The new list is created on the heap; the values are loaded on the stack (i.e. before they are added to the list), but once added, they are on the heap, in the int[] that underpins the list. Arrays are always on the heap.

    The fact that they are copied to the array also answers part 2 I believe. The array is over-sized, and reallocated only when full.

    Note; List doesn't "become" a reference-type; it is always a reference-type.

提交回复
热议问题