StringBuilder.Append vs. concatenation

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

Hi

Would just like to know the difference between the below

StringBuilder.Append
and
strVariable += "string"

Thanks
Jason
 
Hi,
StringBuilder.Append will be faster and efficient if more than two
strings are there to add. If we use the StringBuilder the CLR uses the same
memory location for all append operations. Since strings are immutable, if u
add two string using + sign, it will create seperate memory location........

--
Let me know if you need further help

Regards
Sreejumon[MVP]
www.mstechzone.com
 
Oh ok, that is perfect for what i need

Is there any limit to the size of string that StringBuilder.Append can
handle?

Thanks again
Jason
 
Just the size of strings. Actual size depends upon the platform but is
likely to be around 2GB.

HTH,
 
Hello Jason,
The capacity of a StringBuilder is the maximum number of
characters the instance can store at any given time, and
is greater than or equal to the length of the string
representation of the value of the instance. The capacity
can be increased or decreased with the Capacity property
or EnsureCapacity method, but it cannot be less than the
value of the Length property.

Let me know if you need further help.
Regards,
Rahul Shukla
(e-mail address removed)
 
"By default, StringBuilder will create a string of 16 characters.If the
string needs to grow beyond its capacity, a new string will be constructed
with the greater of twice the previous capacity or the new desired size,
subject to the maxcapacity constraint". This is an extract from a this must
read article in CodeProject: http://www.codeproject.com/dotnet/strings.asp
This article gives a complete insight on StringBuilder.
From what I see, stringbuilder will be very useful if the number of string
concat. operations are high, like concatenations in a for loop. I think, for
simple concatenations, normal string is fine, like "This " + " one".
 
The stringbuilder adds to an array, which you can later spit out as a long
string. The second line creates a new string object. With large loops, it is
better to use a stringbuilder and output the entire string at the end of the
process.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Actually, the cut-off point for performance is around 5-10 string concats.
Creating a new StringBuilder has its own performance hit. So, unless you're
going to add more than a few strings, use the standard way (a + b). Use
StringBuilder when you'll have a loop of 5+ operations.

-mike
MVP
 
Back
Top