Jawahar BABU
Jawahar BABU

Reputation: 1075

Default Capacity of List

What is the default capacity of a List?

Upvotes: 73

Views: 53877

Answers (8)

trongtoannguyen
trongtoannguyen

Reputation: 45

should you use this approach to ensure that any necessary information is correctly updated every version.

List<string> dinosaurs = new List<string>();
Console.WriteLine("\nInit:\nCapacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);

dinosaurs.Add("Deinonychus");
Console.WriteLine();
foreach (string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);

dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Son of Compsognathus");
Console.WriteLine();
foreach (string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);

/* In C# 11 This code example produces the following output:

Init:
Capacity: 0
Count: 0

Deinonychus
Capacity: 4
Count: 1

Deinonychus
Mamenchisaurus
Amargasaurus
Compsognathus
Son of Compsognathus
Capacity: 8
Count: 5

==>> default: 0
*/

Upvotes: 0

Fatih G&#220;RDAL
Fatih G&#220;RDAL

Reputation: 1519

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.

Upvotes: 3

evhen14
evhen14

Reputation: 1927

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

Upvotes: 1

tomkuj
tomkuj

Reputation: 81

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.

Upvotes: 0

Thorarin
Thorarin

Reputation: 48516

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);
    }
}

Upvotes: 75

Jon Skeet
Jon Skeet

Reputation: 1504062

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.

Upvotes: 52

Mark Byers
Mark Byers

Reputation: 839234

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.

Upvotes: 65

Elisha
Elisha

Reputation: 23800

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);

Upvotes: 7

Related Questions