Search code examples
.netstringstringbuilder

Strings and string builder


What is the difference between strings and string builder in .NET?


Solution

  • A string is an immutable type. It has bad performance characteristics when performing lots of string manipulation like concatenation.

    Stringbuilders on the other hand overcome this weakness by keeping a growing buffer so that each concatenation is less likely to require a new string to be allocated.

    Since string builders add some overhead, they are only really necessary when some significant string work is to be done (e.g. in a loop). If your code is fast, don't worry about it. If it's not, use a profiler to see if this issue matters in your case.

    One final note: this answer really has nothing to do with ASP.NET--this is true of strings in all of .net and lots of other languages, too.