What is the default capacity of a List?
Capacity is the number of the elements which the List can store before resizing of List needed. But Count is the number of the elements which are actually present in the List.
The capacity property in ArrayList class gets or sets the number of elements that the ArrayList can contain. The default capacity is 4. If 5 elements are there, then its capacity is doubled and would be 8.
List size can be increased up to 2 billion (only when your system works on 64-bit or higher) to store large List<T> objects.
Default initial capacity of Vector is 10. java. util. Vector default constructor defines size of 10.
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With