Generics memory management

后端 未结 7 753
慢半拍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:07

    Memory management for generics (Generic collections) is exactly the same as for non-generic types.

    Your ints1 list uses an array under the covers. So it is the same as for ints2 (when it has been corrected). In both cases a block of memory on the Heap is holding the int values.

    The List<> class consists of an array, a int Count and an int Capacity property. When you Add() an element Count is incremented, when it passes Capacity a new array is allocated and the contents are copied.

提交回复
热议问题