Re: Stringbuilder vs Strings

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On May 9, 8:35 am, Maverick <Moty...@xxxxxxxxx> wrote:
On May 9, 6:02 pm, "Nicholas Paldino [.NET/C# MVP]"





<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Andy" <a...@xxxxxxxxxxxxxxxxxx> wrote in message

news:1178718261.520466.20550@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

On May 9, 9:39 am, Dara P <D...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Can some one suggest me as why StringBuilder class is better than
Strings?

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.

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.

Correction: N string concatenations will allocate N strings, ONE for
each concatenation, not N for each contatenation. The example that
followed gave the correct numbers.

.



Relevant Pages

  • Re: stringbuilder performance questions
    ... While true for a small number of concatenations, ... fast you can concatenate strings together, ... the net effect is small in many cases. ... Developers for exciting positions in medical product ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: stringBuilder vs string concatenation myth of usage...
    ... more than 4 or 5 strings ... I'm really glad to hear the opinion from someone ... who uses stringbuilder for more than 5 string concatenations. ... it won't do the same job with the same cost. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Best practices for efficient FP string building in CL?
    ... suppose that I want to build a string representing an SVG ... We've all written code like this before, building big strings from ... concatenations is inefficient, ... If one writes in LIsp, then one may write lots of little building blocks. ...
    (comp.lang.lisp)
  • Re: Best practices for efficient FP string building in CL?
    ... We've all written code like this before, building big strings from ... concatenations is inefficient, ... algorithms that can be easily be coded in oshould not be ... If one writes in LIsp, then one may write lots of little building blocks. ...
    (comp.lang.lisp)
  • RE: Datatype Misalignment MFC
    ... Correction, the trace line does specify the strings of course... ... This problem only occurs on SH3 Device. ...
    (microsoft.public.windowsce.embedded.vc)