Forum guidelines
AuthorQuestion
Advantage of using StringBuilder in C#
Posted on: 12 Feb 2012
Hi friends,

I want to know that what's the advantage of using System.Text.StringBuilder over System.String?

AuthorReply
Re: Advantage of using StringBuilder in C#
Posted on: 12 Feb 2012  
The StringBuilder class enables you to (in effect) create a mutable string i.e. a string that can be changed.

You can change existing characters, insert or add new ones or delete characters and it's still the same object. It automatically increases its size when needed by doubling the size of its internal buffer.

In constrast, the String class produces immutable strings. Methods which appear to change the string do in fact return a new one.

Because StringBuilders are mutable they are useful for operations such as concatenation on a large number of strings, where they are more performant than ordinary strings and ease pressure on the garbage collector


Re: Advantage of using StringBuilder in C#
Posted on: 12 Feb 2012  
hi,

string is immutable and you allocate new memory each time you append strings.

StringBuilder allows you to add characters to an object and when you need to use the string representation, you call ToString() on it.


please refer.

http://www.codeproject.com/Articles/6771/String-Vs-StringBuilder-C
http://msdn.microsoft.com/en-us/library/2839d5h5

hopr this help.

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.