473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Strange StringBuilder behavior

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("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaa");
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
Sep 10 '08 #1
5 1305
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
Sep 10 '08 #2
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"
Sep 10 '08 #3
On Sep 10, 4:17*pm, Marc Gravell <marc.grav...@gmail.comwrote:
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?
Sep 10 '08 #4
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/archi...libraries.aspx
Note the MS-RL (linked from that page) applies.

Marc

Sep 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
9
by: Tamir Khason | last post by:
I have some string in variable str_tmp = "C:\TMP\" I want to display it in textbox (@-quoted), so I'm using tbx.Text = @str_tmp; but for some strange reason this does not work. it processes escape...
5
by: Heribert Coumans | last post by:
given the following 2 functions: public bool AnOtherSimpleTest1(string aString) { StringBuilder strB1 = new StringBuilder(); StringBuilder strB2 = new StringBuilder(); strB1.Append(aString);...
5
by: Christof Nordiek | last post by:
Hi, in my Application i have to make a Copy of a StringBuilder, so that, if the new old instance is changed, the you instance will not be affected. I could do something like new...
3
by: ramata | last post by:
Hi I am using StringBuilder to compose an email. The body of email consists of a table for which data has to be taken fron SQL Server. I use append method to add text to the StringBuilder...
8
by: Henning M | last post by:
Hi, I'm trying to use stringbuilder to collect a list of strings. (as suggested by Claes Bergefall) Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" (ByVal pszFilter As String,...
7
by: donnie.hale | last post by:
Perhaps this has been discussed elsewhere - if so, I couldn't find it. The StringBuilder.Length property is explicitly documented as padding the length with spaces if .Length is set to a value...
3
by: Morgan Cheng | last post by:
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use string or StringBuilder. However, there is one problem about StringBuilder. By default, its Capacity is 16. If the returned...
5
by: pantagruel | last post by:
Hi, It is generally stated that stringbuilder should be used instead of just concatenating strings with the plus operator. That's fine enough what I'm wondering in cases I have: String S =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.