When to use StringBuilder for concatenation rule of thumb

20. October 2007

Because String is an immutable type, the variable value can change but the original data value has to be discarded from memory and a new data value has to be created in memory.  StringBuilder, on the other hand, was designed for multiple string operations. 

So, if you have a case where there is a loop and you have to manipulate data in a string more than maybe 10 times, the code will be better off using StringBuilder.  But do use plain old String concatenation if you want to do something simple like this:

string sometext = "You should use " + "StringBuilder" + " in large loops or you are just loopy.";

Code

Comments are closed