interviews.dotnetthread.com

↑ Grab this Headline Animator

Friday, April 9, 2010

What is the difference between System.Text.StringBuilder and System.String?

String is immutable and string builder is mutable.
String is useful as some costly operations for copying and comparing can be omitted making program simple.

However, making an object immutable is usually inappropriate if the object contains a large amount of changeable data.

So String is not suggested to hold huge data as replacements or concatinations will be costly and time consuming.

Copying is simple using reference. Whereas reference copying technique (Copying data without pointing to the same reference) is difficult in case of StringBuilder.

String will allocate only what is needed. When we concatinate strings, it sums up the length of all and creates buffer to fit that.

Whereas string builder allocates capacity of 16 initially and increases capacity in the multiples of 2.

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles