Article ID: 306822 - Last Review: July 14, 2008 - Revision: 3.0 How to improve string concatenation performance in Visual C#
This article was previously published under Q306822 On This PageSUMMARY
This step-by-step article shows you the benefits of using the StringBuilder class over traditional concatenation techniques. Strings in the .NET Framework are invariant (that is, the referenced text is read-only after the initial allocation). This provides many performance benefits and poses some challenges to the developer who is accustomed to C/C++ string manipulation techniques. Description of Strings in the .NET FrameworkOne technique to improve string concatenation over strcat() in Visual C/C++ is to allocate a large character array as a buffer and copy string data into the buffer. In the .NET Framework, a string is immutable; it cannot be modified in place. The C# + concatenation operator builds a new string and causes reduced performance when it concatenates large amounts of text.However, the .NET Framework includes a StringBuilder class that is optimized for string concatenation. It provides the same benefits as using a character array in C/C++, as well as automatically growing the buffer size (if needed) and tracking the length for you. The sample application in this article demonstrates the use of the StringBuilder class and compares the performance to concatenation. Build and Run a Demonstration Application
Troubleshooting
REFERENCES
The StringBuilder class contains many other methods for in-place string manipulation that are not described in this article. For more information, search for "StringBuilder" in the Online Help. | Other Resources Other Support Sites
CommunityGet Help NowArticle Translations
|






Windows Live
Facebook
Twitter
Linkedin
Digg it
Yahoo
Delicious
StumbleUpon
Yammer
Reddit
Technorati
FriendFeed
Email
Back to the top
