C# Tip: Use StringBuilder

5. November 2008 18:31

Okay, as an extension to the last post on String.Format, I'm doing a quick introduction to the System.Text.StringBuilder class.  Why, you ask.  Because when you append to a string, there is a lot of memory being allocated, transferred and dumped behind the scenes.  Say you have a string of "My string is" and you want to append " cool." what happens is a new string is allocated in memory, with enough space for the original string, and the appended value.  Then the original string is copied to the beginning, and the appended string is copied after.  This is very inneficient.  StringBuilder pre-allocates additional space, and handles the concatenation behind the scenes. More...


Michael J. Ryan aka Tracker1

My name is Michael J. Ryan and I've been developing web based applications since the mid 90's.

I am an advanced Web UX developer with a near expert knowledge of JavaScript.