Default Capacity of List

C#.NetListCapacity

C# Problem Overview


What is the default capacity of a List?

C# Solutions


Solution 1 - C#

Actually, it starts with a Capacity of 0. When you add the first element, the current implementation allocates a capacity of 4. After that, the capacity keeps doubling if expansion is needed, to guarantee amortized O(1) operation.

Keep in mind that this is the current behavior. You shouldn't rely on it to be the case. This should demonstrate the current behavior:

List<int> list = new List<int>();
int capacity = list.Capacity;
Console.WriteLine("Capacity: " + capacity);

for (int i = 0; i < 100000; i++)
{
    list.Add(i);
    if (list.Capacity > capacity)
    {
        capacity = list.Capacity;
        Console.WriteLine("Capacity: " + capacity);
    }
}

Solution 2 - C#

Why don't you just try it?

Console.WriteLine("Default capacity of a List: " + new List<int>().Capacity);

This answer will work on all versions of .NET that have List. On my version, it happens to be 0.

Solution 3 - C#

According to the sample on the MSDN parameterless constructor documentation, the initial capacity of a list created with:

List<string> x = new List<string>();

is 0. As far as I can tell, this isn't documented as a guarantee, nor is the resize policy documented (i.e. it may currently double with a minimum of 4, but in .NET 5.0 it could triple with a minimum of 128.) You shouldn't rely on this behaviour, basically.

Solution 4 - C#

The default capacity of List is 4 items (after you insert an initial item, otherwise it's of 0 size)

var list = new List<int>();
list.Add(1);

Assert.AreEqual(4, list.Capacity);

Solution 5 - C#

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.

Solution 6 - C#

Capacity should be used if you know roughly how many items you want to store in List (or in Stack, or Queue).

In this case you will avoid memory copying. The memory copying happens because under the hood Lists (Stacks and Queues) rely on array to store their items. That array size is you capacity, but it's not the same as the list size. As size of list needs to be bigger than the size of array, the List implementation will allocate a bigger array (factor of 2 maybe less) and will copy all items from old array to the new one plus newly added items.

So, if you know that you may have from, say, 50 to 60 items in your list, create a list with capacity 60 and no memory deallocation will happen.

Note: And it looks like Garbage Collector will not have to clean up old arrays

Solution 7 - C#

This all lies in one line for ensuring the capacity is able to store another element:

int num = this._items.Length == 0 ? 4 : this._items.Length * 2;

Got this from the mscorlib 4.0.0.0 deassebled - of course, as Jon said, this cannot be guaranteed not to change in the future (so far it still stays at 0, 4, 8, 16...).

Of course, you could set it yourself so this can be 3, 9, 27 etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJawahar BABUView Question on Stackoverflow
Solution 1 - C#ThorarinView Answer on Stackoverflow
Solution 2 - C#Mark ByersView Answer on Stackoverflow
Solution 3 - C#Jon SkeetView Answer on Stackoverflow
Solution 4 - C#ElishaView Answer on Stackoverflow
Solution 5 - C#Fatih GÜRDALView Answer on Stackoverflow
Solution 6 - C#evhen14View Answer on Stackoverflow
Solution 7 - C#tomkujView Answer on Stackoverflow