G
Guest
Can some one suggest me as why StringBuilder class is better than Strings?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Can some one suggest me as why StringBuilder class is better than Strings?
Can some one suggest me as why StringBuilder class is better than Strings?
It should be said that you should use StringBuilder to concatenate when
you don't exactly know how many strings you are going to concatenate. The
best example is when you have N number of strings and you need to append it
all together in a loop.
If you have a known number of strings, then using the concatenation
operator will work just fine, as it will compile to a call to the static
Concat method on the string class, which is very fast (it pre-allocates the
return buffer, and then does a memory copy of the strings into the
appropriate locations in the return buffer).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.

Dara said:Can some one suggest me as why StringBuilder class is better than Strings?
Hello,
String class is immutable. Meaning any changes made to the reference
you have to a String object (such as concatenations), will be made on
a copy of the reference. Thus, as Nicholas mentioned, N strings
concatenations will allocate N strings for each concatenation.
Chakravarthy said:In simple, when you use the Strings manipulation, Boxing and unboxing
happens.
Where as when you use the Stringbuilder, as it clearly knows that
deals with Chars, so CLR will not undergo all the conversion and allocation
of extra memory bla.. bla..
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.