when there is stringbuilder, why string ?

  • Thread starter Thread starter medhanush
  • Start date Start date
M

medhanush

Hi,

one question on why string is provided ?

stringbuilder offers more performance, less work for GC and good amount
of methods to manage text in it.

When there are many benefits with stringbuilder, why still string is
supported in framework ?

is it only for any compatibility and threadsafe nature of string ?

TIA
MeDhanush
 
one question on why string is provided ?

stringbuilder offers more performance, less work for GC and good amount
of methods to manage text in it.

When there are many benefits with stringbuilder, why still string is
supported in framework ?

is it only for any compatibility and threadsafe nature of string ?

No. Strings are immutable, which is invaluable. StringBuilders should
be used as their names suggest - to *build* strings. The final form
should be as a string though.

Note that it's not always more efficient to use StringBuilder, by the
way - see http://www.pobox.com/~skeet/csharp/stringbuilder.html
 
... and don't forget they are quite easy to use.

string s = "Hello World!"; //neat
string s = new StringBuilder().Append("Hello World!").ToString(); // yuk

Happy Coding
- Michael S
 
I think the problem has been the numerous authors who have, by rote, said
"use StringBuilder, not String", but as Jon Skeet pointed out: StringBuilder
is only a wise choice in the right context.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 
Back
Top