If I set the initial size of the .NET collection and then add some elements on top of this initial size, how will the collection determine the next size?

I started trying to set the initial size of some of my .NET collections when we know some expected maximum sizes. Think of pre-caching static data, etc.

If we start adding items to this collection later, the collection will resize to fit these new items. How to determine what the next size will be? I thought that if we use the default logic and do not set the initial size, it will resize based on some pre-encoded logic. Now, when we set the initial size, this screw, which is logically installed?

Is it simplified, and some kind of mathematical formula like current size + 10%or something?

0
source share
1 answer

In the <T> implementation list, the algorithm should start the size at 4, and then double it as soon as the size is reached. Best practice is to distribute a <T> list with size during construction. It is more efficient to allocate size and memory in advance if you know the size. See http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs

+1
source

All Articles