Strange StringBuilder behavior

G

Gancy

PROGRAM

StringBuilder sbTest1 = new StringBuilder();
StringBuilder sbTest2 = new StringBuilder();

for (int i = 1; i <= 54; i++)
sbTest1.Append("a");
System.Diagnostics.Debug.WriteLine("sbTest1 => Capacity: "
+ sbTest1.Capacity + ", Length: " + sbTest1.Length);


sbTest2.Append("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
System.Diagnostics.Debug.WriteLine("sbTest2 => Capacity: "
+ sbTest2.Capacity + ", Length: " + sbTest2.Length);


OUTPUT

sbTest1 => Capacity: 64, Length: 54
sbTest2 => Capacity: 54, Length: 54


QUESTION

as seen from the output if the string is appeneded to StringBuilder
(sbTest1) from loop the capacity increases by multiple of two and
Lenght would show as many charecters are in the string. If appended
from a statement outside loop as done with sbTest2, would show same
Length and Capacity.

I can not understand the reason as why this behaviour? is this
intentional for some good reason or a bug
 
M

Marc Gravell

The capacity starts (by default) as 16.

When adding data (that exceeds the capacity), the algorithm first
checks to see if doubling the capacity would be enough - if not it
just uses the new length. So with test2, doubling gives 32, which
isn't enough - so it allocates 54.

With test1, when it gets to the 17th item, it doubles to 32 - and this
is fine until the 33rd item when it doubles to 64.

Make sense? This approach attempts to minimise the number of times the
string has to be reallocated.

Marc
 
M

Marc Gravell

Actually - I should add that all of this is "implementation details" -
fun* to know, but don't rely on it - just know that the StringBuilder
will make suitable attempts to work efficiently with the strings.

Marc

*=for small values of "fun"
 
G

Gancy

Actually - I should add that all of this is "implementation details" -
fun* to know, but don't rely on it - just know that the StringBuilder
will make suitable attempts to work efficiently with the strings.

Marc

*=for small values of "fun"

Thanks Marc. I was wondering why could this be. But generally
speaking how would a developer learn\know about these implementation
details for curiosity reasons? any pointers where I can know about
these?
 
M

Marc Gravell

Sometimes it says in the documentation.

In this case (http://msdn.microsoft.com/en-us/library/
system.text.stringbuilder.aspx) it explicitly states that it is
"implementation specific", so if you want to know what it *actually*
does you have a few options:

1: look in Red Gate's .NET Reflector -
http://www.red-gate.com/products/reflector/
Note that *technically* there may be a few issues with disassembling
code - but it is the quickest way of answering questions like this,
and is sometimes invaluable. I use reflector as one of my core tools,
especially on my own code - ironic that it is quicker to look at a dll
in reflector than it is to look at it in VS!

2: use the Microsoft symbol server
http://weblogs.asp.net/scottgu/arch...rce-code-for-the-net-framework-libraries.aspx
Note the MS-RL (linked from that page) applies.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top