Default capacity of StringBuilder
Asked Answered
S

8

38

What is the default capacity of a StringBuilder?

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

Surrounding answered 29/10, 2008 at 9:23 Comment(0)
G
6

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

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

Globigerina answered 29/10, 2008 at 9:36 Comment(4)
That's a good reminder that I should edit the article to include the word "capacity" somewhere :)Massenet
Good article, but doesn't answer either question.Foveola
@JonSkeet - still missing the word capacity. Interesting article though.Brianabriand
This answer does not include any detail, just a link.Carnal
C
50

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

Coparcener answered 29/10, 2008 at 9:36 Comment(0)
A
37

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.

Allisan answered 29/10, 2008 at 11:30 Comment(0)
H
15

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.

Humo answered 15/12, 2011 at 16:35 Comment(0)
G
6

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

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

Globigerina answered 29/10, 2008 at 9:36 Comment(4)
That's a good reminder that I should edit the article to include the word "capacity" somewhere :)Massenet
Good article, but doesn't answer either question.Foveola
@JonSkeet - still missing the word capacity. Interesting article though.Brianabriand
This answer does not include any detail, just a link.Carnal
P
2

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 !!!

Photoengraving answered 11/10, 2017 at 6:15 Comment(3)
Can you add a link to the documentation where this is confirmed?Buttaro
I have tried in my project. If u want to try out then u can practice it in dotnetfiddle.net.Photoengraving
String Builder Class(System.Text) | Microsoft Docs for referenceHightail
A
1

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.

Anteroom answered 20/9, 2013 at 5:11 Comment(0)
G
-1

[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.

Gemmell answered 29/10, 2008 at 9:25 Comment(1)
I think he did mean StringBuilder, nothing else.Varian
A
-1

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

Ar answered 29/10, 2008 at 9:36 Comment(1)
That linked article refers to maximum capacity. Not the default capacity (16).Surrounding

© 2022 - 2024 — McMap. All rights reserved.