Possible Duplicate:
String vs StringBuilder
Hi,
I'm creating a json string. I have some json encoders that receive objects and return json string. I want to assemble these strings into one long string.
What's the difference between using a string builder and declaring a string an appending strings to it.
Thanks.
When you append to a string, you are creating a new object each time you append, because strings are immutable in .NET.
When using a StringBuilder, you build up the string in a pre-allocated buffer.
That is, for each append to a normal string; you are creating a new object and copying all the characters into it. Because all the little (or big) temporary string objects eventually will need to get garbage-collected, appending a lot of strings together can be a performance problem. Therefore, it is generally a good idea to use a StringBuilder when dynamically appending a lot of strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With