-3

Is there a significant difference between String and StringBuilder in C# and when would you use one over the other?

Example:

Using String string stringVal = "Hello World!"

or we can do it using StringBuilder

StringBuilder sbMyValue = new StringBuilder("");
sbMyValue.Append("Hello World");
AnonDCX
  • 2,501
  • 2
  • 17
  • 25
  • `StringBuilder` allows more efficient concatenation of strings. If you concatenate multiple individual strings you wind up making lots of copies of the strings involved (as they are immutable in C#), whereas with `StringBuilder` only one copy is made. In your example above there is practically no difference, and there might be a little more overhead with `StringBuilder`. – Tim Sep 11 '15 at 03:12
  • its a performance thing. it is probably best said here. [http://stackoverflow.com/questions/73883/string-vs-stringbuilder][1] [1]: http://stackoverflow.com/questions/73883/string-vs-stringbuilder – Jonathan cartwright Sep 11 '15 at 03:14
  • `Is there a significant difference between String and StringBuilder in C#` Yes absolutely – Jonesopolis Sep 11 '15 at 03:17

2 Answers2

1

StringBuilder is mutable which gives better performance when you need to manipulate content multiple times.

In case of string, it has to create instances multiple times because string is immutable.

Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
0

this article explains the difference between the two neatly: link
to summarize :
String

String is immutable, Immutable means if you create string object then you cannot modify it and It always create new object of string type in memory. Stringbuilder

StringBuilder is mutable, means if create string builder object then you can perform any operation like insert, replace or append without creating new instance for every time.it will update string at one place in memory doesnt create new space in memory. So, String builder saves a lot of memory when it comes to concatenation.

Nikita Shrivastava
  • 2,978
  • 10
  • 20