Default capacity of StringBuilder

This question came up today as a duplicate of another, but I notice one part wasn't answered. The default (assuming that means "when not created with a string that is large enough to require ) is 16 as people said, but I don't see anything here on when you should change it.

You change it when you can do so as a possible optimisation. Indeed, the choice of 16 is the opposite to an optimisation. Optimisation is picking values and approaches so as to particularly well suit a particular case or subset of possible cases, (not "making things faster" generally, though that's how we often use the word). Here the designer of the class had to deal with generalisation - picking values and approaches so as to give reasonably good performance across a broad range of cases.

The smaller they went, the less use of memory.

The larger they went, the less reallocation to deal with larger strings.

There's a few reasons why binary-round (whole powers of two) are likely to give better performance than other numbers in certain cases, so they went for one of those, but that aside the choice between 4 or 16 or 1024 was a matter of balancing different likely values.

Someone using StringBuilder rather than designing it, may have a better idea of what size they are likely to need.

If they are going to Append 5 1-digit numbers along with strings that total to 43 characters in length, then the total length of the StringBuilder is going to be 48 characters no matter what, so they should use a capacity of 48 as 48 is always the most efficient size for a string of 48 length.

If they are doing something where there could be any length between about 23 and 34 chars, they should use 34.

If they are doing something where there will likely never be more than 60 chars, but every now and then there could be, they should use 64 (don't reallocate for most parts, and gain the power-of-two benefit mentioned above for the few cases where you do).

If it's impossible to come to a conclusion on this, or at least hard to do so and not a performance hot-spot, then you should just use the default.


The default capacity of StringBuilder is 16 characters (I used .NET Reflector to find out).


Default is 16, which seems to be the default capacity of any type of array or list in the .NET framework. The less number of reallocations you need on your StringBuilder, the better it is. Meanwhile, it is unnecessary to allocate much more than is needed, too.

I usually instantiate the StringBuilder with some type of rough estimate as to the final size of the StringBuilder. For instance, this could be based on some iteration count that you will use later on to build the string, times the size needed for each item in this iteration.

// where 96 is a rough estimate of the size needed for each item
StringBuilder sb = new StringBuilder ( count * 96 );
for ( int i = 0; i < count; i++ )
{
...
}

When the size of a StringBuilder is too small for writing the next string, the internal char array of StringBuilder is reallocated to twice its current size.