Whats the use of string builder?

Answer Posted / kishoreg

Since C# strings are immutable, an existing string cannot
be modified. So, if one tries to change a string either
with the concatenation operator (+) or with the Insert,
PadLeft, PadRight, Replace, or SubString methods, an
entirely new string is created—leaving the original string
intact.

Therefore, operations which would alter strings—instead—
cause additional memory to be allocated. Memory is a scarce
resource. And, memory allocations are expensive in terms of
memory and performance. Consequently, sometimes String
class usage should be avoided.

The StringBuilder class is designed for situations when one
needs to work with a single string and make an arbitrary
number of iterative changes to it. Many StringBuilder class
methods are the same as those of the String class. However,
the string content of a StringBuilder class can be changed
without the necessity of allocating additional memory.
Thus, operations on the StringBuilder class will be much
faster than operations on the String class in certain
situations. Paradoxically, just the the opposite can be
true in other situations.

The String class is optimized and quite efficient for most
cases. On the other hand, if strings must be modified, then
the String class can be a real resource waster. It must be
appreciated that the String class is really very
intelligent in its memory handling in most everyday
programming situations.

Instead of the String class, use the StringBuilder class
when a single string must be modified repeatedly.

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of console?

562


What is hashtable in c# net with example?

566


What are the properties of a string class?

590


Can you change the value of a constant filed after its declaration?

596


Which property do we set on a Combo Box to display data on it prior to setting the DataSource ?

619






What is typeof undefined?

586


Is array reference type in c#?

568


What tool we have to use to install assembli in gac folder.

557


what are windows services?

601


What is the meaning of extention?

557


What is difference between ilist and list?

563


What is Custom attribute? How to create? If I'm having custom attribute in an assembly, how to say that name in the code?

601


In which order the destructor is called for an inherited class?

604


What is the difference between properties and indexer in c#?

507


What is a race condition?

567