Google

Wednesday, November 29, 2006

Stringbuilder - .Net

String object is immutable. Every time you use one of the methods in the System.String class it creates a new string object in memory, which requires a new allocation of space for the newly created string object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly.

allocated memory in some location say 4253
string str = "BlogHead";
erases contents at 4253 and creates new memory location say 8578
str += "geeks";

Any modifications you do to the string object memory will change.

The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

No comments: