I am declaring a list like below and then I am adding 12 items to it:
List lstPolygonWkt = new List();
foreach (var i in items)
lstPolygonWkt.Add(i.PolygonWkt);
One should think that the list now contains 12 elements, right?
But to my surprise it turns out that the list suddenly contains 16 items and then the last 4 items are null.
I don’t understand why my list which should be 12 items is suddenly 16 items. Any idea why? And how to make the list only 12 items as it should be?
I will paste a couple of screen shots:


The list reserves memory in chunks everytime it needs to grow in capacity. Hence capacity reports 16, but count only reports 12. Null items contribute towards the count.
The list class provides the
TrimExcessmethod to remove unutilised space.Also, specifying the capacity upfront in the constructor results in only one memory grab ( assuming you don’t exceed that capacity).
Your screenshot shows a count of 12 with a capacity of 16. If memory serves, the list attempts to double its size (or at the very least definitely defaults to 4, then goes to 8, then 16). As you have 12 items, you triggered the jump from 8 to 16 capacity.