Whats the use of string builder?



Whats the use of string builder?..

Answer / 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

More C Sharp Interview Questions

What does console readline do in c#?

0 Answers  


What is static classes?

0 Answers  


int i = 1; int j = 1; System.Console.WriteLine(i == j); System.Console.WriteLine(i.ToString() == j.ToString()); System.Console.WriteLine((object)i == (object)j); Give the sample code above, what is the output to the console?

5 Answers  


what is the difference between passing a value object by reference and a reference object by value?

4 Answers   TCS,


Define a partial class?

0 Answers  






How to create events for a control? What is custom events? How to create it?

0 Answers  


What is ildasm.exe used for?

0 Answers  


Are structs value types or reference types?

0 Answers  


What is the use of readkey in c#?

0 Answers  


Error handling and how this is done ?

2 Answers   Digital GlobalSoft,


Explain deadlock?

0 Answers  


Define collections?

0 Answers  


Categories