What is the difference between strings and string builder in .NET?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.