Which algorithm is used in List to dynamically allocate memory?

后端 未结 6 1203
渐次进展
渐次进展 2021-01-18 22:18

Now I have an algorithm for dynamically allocating memory on an array:

  • If array is full I create a new array of twice the size, and copy items.
  • If arr
6条回答
  •  梦毁少年i
    2021-01-18 23:17

    Yes, List uses a T[] internally to hold your objects.

    As far as I remember from the .NET 4.0 source code, before adding new objects, it ensures that the array has enough capacity to hold the new number of objects. If the existing array cannot hold the new number of objects, it is replaced by a new array of double the size and all the objects and all the existing references are copied to the new array.

提交回复
热议问题