Default Capacity of List

Viewed 44725

What is the default capacity of a List?

7 Answers

The capacity default is 0, but if you create a blank list [List1] as below. If the list you created has elements in [List2] as follows, the number of the elements you add becomes N over 2. The default capacity varies.

List<int> List1 = new List<int>(); //count=0, capacity=0
List<int> List2 = new List<int>(){ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //count=11, capacity=16

When the elements of Array type were added, the developers expanded the array with the ReSize method. It worked very slowly. While developing the List type, the capacity was increased to a certain extent. This ratio has been developed as N above 2.

If more than the number of members are added to the list, the current capacity is doubled. Capacity will not decrease if you delete some values from the list. Capacity only increases, not decreases. Even the Clear method does not affect it.

Related