Question

What is the default capacity of a StringBuilder?

And when should (or shouldn't) the default be used?

Was it helpful?

Solution

The Venerable J. Skeet has provided a good analysis of precisely this problem:

https://jonskeet.uk/csharp/stringbuilder.html

OTHER TIPS

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.

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.

We can find the capacity using the capacity property of the StringBuilder Class:

StringBuilder builder = new StringBuilder();
var capacity = builder.Capacity;
var maxCapacity = builder.MaxCapacity;

Here capacity defines the default capacity of StringBuilder.

StringBuilder is Max Capacity is same as the maximum value of Int32.

[edit: at the time, the question asked about StringList]

Do you mean StringCollection? This uses an empty ArrayList initially, so the answer is 0. And you have no option to change it. When you first add an item, the capacity jumps to 4, then uses a doubling strategy when it fills.

If you mean List<string>, then it is similar (an empty T[], not an ArrayList), but you can initialize a known size if you need (i.e. you know how much data you expect). Again, the first time you Add to a List<T> the size jumps to 4, then doubles every time it fills up.

Default capacity of a String-Builder is 16 characters , And Max capacity of String-Builder is 2147483647 characters.

So no need to worry for storing long response !!!

read hear about Stringbuilder capacity , there is sample app to prove it too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top