Home > Software > .NET > C-Sharp
Interview Questions   Tutorials   Discussions   

C-Sharp - What is the Main difference between String and StringBuilder and why do we use StringBuilder?




281
views
asked mar October 4, 2014 04:19 AM  

What is the Main difference between String and StringBuilder and why do we use StringBuilder?


           

1 Answers



 
answered By siddhant   0  

A StringBuilder will help you when you need to build strings in multiple steps.

Instead of doing this:

String x = "";
x += "first ";
x += "second ";
x += "third ";

you do :-

StringBuilder sb = new StringBuilder("");
sb.Append("first ");
sb.Append("second ");
sb.Append("third");
String x = sb.ToString();

the final effect is the same, but the StringBuilder will use less memory and will run faster. Instead of creating a new string which is the concatenation of the two, it will create the chunks separately, and only at the end it will unite them.

flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert