473,387 Members | 1,529 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,387 software developers and data experts.

Stringbuilder, how does it internally work ?


I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.
Thanks,
Nov 15 '05 #1
12 7371
<an*******@discussions.microsoft.com> wrote:
I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.


The *exact* details are quite tricky - I believe it internally has a
string which it changes, creating a new string of larger capacity when
it needs to.

The *basic* details, however, are that it's effectively like an
ArrayList but for chars. If you understand how ArrayList works, that
should give you a good feeling for StringBuilder.

In your case, if you're creating the StringBuilder, appending a lot of
text to it, and then *only* reading from it, I'd convert it to a String
(using ToString) before you start reading from it. That will make the
code easier to understand for one thing. (Most developers know about
reading from a string in various ways, but not reading from a
StringBuilder.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Basically, the StringBuilder holds an array of bytes internally (I'm
guessing, but it can't hold stirngs, since strings are immutable, so it can
just store the byte representation of the string). When the capacity is
exceeded, then a new buffer is allocated.

If you know that you are definitely going to have text of that size in
thye StringBuilder, then for the best performance, you might want to
pre-allocate the buffer that is used internally. Basically, set the
Capacity property to a value that is above what you are going to need on
average. This way, in most cases, the internal buffer will be allocated to
hold what you need.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<an*******@discussions.microsoft.com> wrote in message
news:09****************************@phx.gbl...

I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.
Thanks,

Nov 15 '05 #3
Jon,

It can't use a string internally, because of the immutable nature of
strings in .NET.

Unless of course by string, you don't mean an actual instance of
System.String, but rather, an array of characters.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<an*******@discussions.microsoft.com> wrote:
I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.


The *exact* details are quite tricky - I believe it internally has a
string which it changes, creating a new string of larger capacity when
it needs to.

The *basic* details, however, are that it's effectively like an
ArrayList but for chars. If you understand how ArrayList works, that
should give you a good feeling for StringBuilder.

In your case, if you're creating the StringBuilder, appending a lot of
text to it, and then *only* reading from it, I'd convert it to a String
(using ToString) before you start reading from it. That will make the
code easier to understand for one thing. (Most developers know about
reading from a string in various ways, but not reading from a
StringBuilder.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
It can't use a string internally, because of the immutable nature of
strings in .NET.

Unless of course by string, you don't mean an actual instance of
System.String, but rather, an array of characters.


Nope, I mean a string.

Strings don't have to be *absolutely* immutable in .NET - they just
have to be *publicly* immutable, and only tampered with *very, very*
carefully within the assembly which defines the string type - and I
believe that's exactly the case.

If you look at the Rotor BCL source code, you'll see how it can be
done. Reading Don Box's "Essential .NET volume 1" book suggests that
it's how .NET does it too.

(There's also a web page about this kind of thing, but I can't find it
at the moment.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
I recent created a set of classes that add low-level binary value-types to
C#, MC++ (Not VB). I wanted to make them immutable because it seems
everytimed I overload an operator I have to return a new instance of the
object with the new value. The Stringbuilder is mutable so I studied its
internal workings for quite some time.
http://www.visualassembler.com/binary

There is a reason why we have to use Appen instead of +=. Internally, the
StringBuilder is an array of bytes. When you Append or do any other
operation on the object, that array is manipulated rather than reallocating
the string an returning it. There is no special magic taking place. No
undocumented "API"'s or anything like that. Just a plain, boring array of
chars. But it works. So there is the secret.
Thanks,
Shawn

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Ox**************@tk2msftngp13.phx.gbl...
Basically, the StringBuilder holds an array of bytes internally (I'm
guessing, but it can't hold stirngs, since strings are immutable, so it can just store the byte representation of the string). When the capacity is
exceeded, then a new buffer is allocated.

If you know that you are definitely going to have text of that size in
thye StringBuilder, then for the best performance, you might want to
pre-allocate the buffer that is used internally. Basically, set the
Capacity property to a value that is above what you are going to need on
average. This way, in most cases, the internal buffer will be allocated to hold what you need.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<an*******@discussions.microsoft.com> wrote in message
news:09****************************@phx.gbl...

I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.
Thanks,


Nov 15 '05 #6
Shawn,

Actually, the string builder is not an array of bytes... See Jon Skeet's
post for more information.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Shawn B." <le****@html.com> wrote in message
news:eE**************@TK2MSFTNGP12.phx.gbl...
I recent created a set of classes that add low-level binary value-types to
C#, MC++ (Not VB). I wanted to make them immutable because it seems
everytimed I overload an operator I have to return a new instance of the
object with the new value. The Stringbuilder is mutable so I studied its
internal workings for quite some time.
http://www.visualassembler.com/binary

There is a reason why we have to use Appen instead of +=. Internally, the
StringBuilder is an array of bytes. When you Append or do any other
operation on the object, that array is manipulated rather than reallocating the string an returning it. There is no special magic taking place. No
undocumented "API"'s or anything like that. Just a plain, boring array of
chars. But it works. So there is the secret.
Thanks,
Shawn

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:Ox**************@tk2msftngp13.phx.gbl...
Basically, the StringBuilder holds an array of bytes internally (I'm
guessing, but it can't hold stirngs, since strings are immutable, so it

can
just store the byte representation of the string). When the capacity is
exceeded, then a new buffer is allocated.

If you know that you are definitely going to have text of that size in thye StringBuilder, then for the best performance, you might want to
pre-allocate the buffer that is used internally. Basically, set the
Capacity property to a value that is above what you are going to need on
average. This way, in most cases, the internal buffer will be allocated

to
hold what you need.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<an*******@discussions.microsoft.com> wrote in message
news:09****************************@phx.gbl...

I know how to use a StringBuilder, which supposedly does
not create a new copy of it each time you modify it
contents by adding or removing text.
But, I wonder how does it do that internally ?
I was planning to use a stringbuilder to hold big amounts
of text, with several megs o size, to be read later based
on fixed offsets, so I need to know if this is suitable
for it.
Thanks,



Nov 15 '05 #7
Shawn B. <le****@html.com> wrote:
I recent created a set of classes that add low-level binary value-types to
C#, MC++ (Not VB). I wanted to make them immutable because it seems
everytimed I overload an operator I have to return a new instance of the
object with the new value. The Stringbuilder is mutable so I studied its
internal workings for quite some time.
How did you go about studying those internal workings, out of interest?
http://www.visualassembler.com/binary

There is a reason why we have to use Appen instead of +=. Internally, the
StringBuilder is an array of bytes. When you Append or do any other
operation on the object, that array is manipulated rather than reallocating
the string an returning it. There is no special magic taking place. No
undocumented "API"'s or anything like that. Just a plain, boring array of
chars. But it works. So there is the secret.


I beg to differ - and now I've found the page which describes a lot of
it in detail:

http://www.codeproject.com/dotnet/strings.asp

<quote>
StringBuilder will construct a string object (which, you thought, were
immutable) and modify it directly.
</quote>

Note that there's no reason why it would count as an "undocumented
API" for String to be modifiable internally to the defining assembly
any more than any other internal member would count as an "undocumented
API". It's unavailable for other assemblies to use, which is why we
don't get to see the documentation for it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Well, one need look no further than ILDASM on mscorlib.dll. Rotor. Mono
(appears to be very similar in its implimentation to what I see in IL when
DASM'd). What is described in the article link you provide, I don't see it
in the ILDASM when looking at the System.Text.StringBuilder object.
Thanks,
Shawn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Shawn B. <le****@html.com> wrote:
I recent created a set of classes that add low-level binary value-types to C#, MC++ (Not VB). I wanted to make them immutable because it seems
everytimed I overload an operator I have to return a new instance of the
object with the new value. The Stringbuilder is mutable so I studied its internal workings for quite some time.


How did you go about studying those internal workings, out of interest?
http://www.visualassembler.com/binary

There is a reason why we have to use Appen instead of +=. Internally, the StringBuilder is an array of bytes. When you Append or do any other
operation on the object, that array is manipulated rather than reallocating the string an returning it. There is no special magic taking place. No
undocumented "API"'s or anything like that. Just a plain, boring array of chars. But it works. So there is the secret.


I beg to differ - and now I've found the page which describes a lot of
it in detail:

http://www.codeproject.com/dotnet/strings.asp

<quote>
StringBuilder will construct a string object (which, you thought, were
immutable) and modify it directly.
</quote>

Note that there's no reason why it would count as an "undocumented
API" for String to be modifiable internally to the defining assembly
any more than any other internal member would count as an "undocumented
API". It's unavailable for other assemblies to use, which is why we
don't get to see the documentation for it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #9
Shawn B. <le****@html.com> wrote:
Well, one need look no further than ILDASM on mscorlib.dll. Rotor.
What member of Rotor are you looking at? I'm looking at the member
m_StringValue, which is what gets updated. (Look at Append(char) for
instance, calling String.AppendInPlace.)
Mono (appears to be very similar in its implimentation to what I see in IL when
DASM'd). What is described in the article link you provide, I don't see it
in the ILDASM when looking at the System.Text.StringBuilder object.


With respect, I still think you're definitely wrong.

Out of interest, which version of the framework are you looking at, and
what particular member is it that hold the char array in StringBuilder?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
Shawn B. <le****@html.com> wrote:
Well, one need look no further than ILDASM on mscorlib.dll. Rotor. Mono
(appears to be very similar in its implimentation to what I see in IL when
DASM'd). What is described in the article link you provide, I don't see it
in the ILDASM when looking at the System.Text.StringBuilder object.


I've just downloaded the Mono 0.28 source, and that does indeed use a
char array in StringBuilder. The Rotor source is entirely different.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
Shawn B. <le****@html.com> wrote:
Well, one need look no further than ILDASM on mscorlib.dll. Rotor. Mono
(appears to be very similar in its implimentation to what I see in IL when
DASM'd). What is described in the article link you provide, I don't see it
in the ILDASM when looking at the System.Text.StringBuilder object.


Rather than use ILDASM, I've written the following program which
probably isn't quite sailing quite as close to the wind in terms of the
EULA.

using System;
using System.Reflection;
using System.Text;

public class Test
{
static void Main()
{
foreach (FieldInfo field in typeof(StringBuilder).GetFields
(BindingFlags.NonPublic|
BindingFlags.Public|
BindingFlags.Instance))
{
Console.WriteLine ("{0} ({1})",
field.Name,
field.FieldType);
}
}
}
On my machine (.NET v1.1) this produces:

m_currentThread (System.Int32)
m_MaxCapacity (System.Int32)
m_StringValue (System.String)

What does it produce on yours?

(This is my final reply to that particular post :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12
StringBuilder uses an internal string defined as: internal string
m_StringValue;

"string" ultimately uses an array of chars and keeps a pointer to the first
char and the length to manage it.
SB also uses internal methods of the "string" class to help it.
public StringBuilder Append(string value)
{
....
//AppendInPlace
//ReplaceTheString
....
}

--
William Stacey, MVP

"Shawn B." <le****@html.com> wrote in message
news:uj**************@TK2MSFTNGP11.phx.gbl...
Well, one need look no further than ILDASM on mscorlib.dll. Rotor. Mono
(appears to be very similar in its implimentation to what I see in IL when
DASM'd). What is described in the article link you provide, I don't see it in the ILDASM when looking at the System.Text.StringBuilder object.
Thanks,
Shawn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Shawn B. <le****@html.com> wrote:
I recent created a set of classes that add low-level binary value-types
to
C#, MC++ (Not VB). I wanted to make them immutable because it seems
everytimed I overload an operator I have to return a new instance of
the object with the new value. The Stringbuilder is mutable so I studied
its internal workings for quite some time.


How did you go about studying those internal workings, out of interest?
http://www.visualassembler.com/binary

There is a reason why we have to use Appen instead of +=. Internally, the StringBuilder is an array of bytes. When you Append or do any other
operation on the object, that array is manipulated rather than reallocating the string an returning it. There is no special magic taking place. No undocumented "API"'s or anything like that. Just a plain, boring
array of chars. But it works. So there is the secret.


I beg to differ - and now I've found the page which describes a lot of
it in detail:

http://www.codeproject.com/dotnet/strings.asp

<quote>
StringBuilder will construct a string object (which, you thought, were
immutable) and modify it directly.
</quote>

Note that there's no reason why it would count as an "undocumented
API" for String to be modifiable internally to the defining assembly
any more than any other internal member would count as an "undocumented
API". It's unavailable for other assemblies to use, which is why we
don't get to see the documentation for it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #13

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

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
8
by: | last post by:
Hi! I'm very interesting in when to use exactly the StringBuilder? For example for something like this?: String strTest1 = "This"; String strTest2 = "Test"; StringBuilder stbTest = new...
9
by: cody | last post by:
Why isn't there an StringBuilder.Append() overload with a StringBuilder as argument? Is there a reason that I missed?
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
6
by: Jim Heavey | last post by:
Hello, I a m building an email message in a StringBuilder Object and then using the StringBuilder ToString() method to get it to regular text. It appears to me that when I place a "\n" into the...
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,...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
7
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, when I concatenate strings is it faster to use the StringBuilder or the string.Format function? thanks christian
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.