It is used to pre-allocate the internal array used by the list. (The size of this internal array is given by List.Capacity
.)
However, just because this internal array is of a certain size, doesn't mean that those elements of the list are available; they are not, until you (for example) use List.Add()
to add the appropriate elements. The current addressable size of the list is given by List.Count
.
When you add a new element to the list, it first checks if there's enough space in the internal array and if there is it just increments an internal counter and uses a slot from the internal array.
If there is not enough space, it allocates a new buffer twice the size of the old one, copies all the elements from the old buffer into the new one, and then discards the old buffer. It can then use a slot from the new buffer. This is obviously quite an expensive operation.
By setting an initial size, you can avoid some (or all) buffer reallocations.
A common use is when you know the maximum possible size of a list, but not the minimum size, and you want to fill the list while performing as few reallocations as possible.