What?s the difference between System.String and
System.StringBuilder classes?

Answer Posted / sandeep joshi

String and StringBuilder class stores strings. But when you
cannot change a String object after creating one.
eg: String name = "Joshi";
By saying you cannot change the name object means you
cannot change the value in name object internally. When you
change the name object value to something else, a new
String object is creating in memory and assign the new
value.

eg: name = "Sandeep Joshi";

A new name object is creating in memory and the
value "Sandeep Joshi" is assinging to the newly created
space.

But StringBuilder class occupies the same space even if you
change the value.
If you are doing string concatenation StringBuilder class
is far better in performance than String class.
You can use StringBuilder's Append() method to use
concatenation and also use Reverse,Remove methods.
Usually, String is immutable which means once you have the
content there you cannot change it. While StringBuilder is
mutable because it allows you to change the content you
have already placed.

Notice that a string allows you to change the content but
offcourse it creates a new string reference. For example,
all string methods return a new string and don't update the
same string variable.

Is This Answer Correct ?    10 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is executereader in c#?

651


What is arraylist?

691


What is difference between array and list?

688


How to get the sum of last 3 items in a list using lambda expressions?

692


What is a statement c#?

683


What does out mean in c#?

698


What is c# best for?

696


Write one code example for compile time binding and one for run time binding? What is early/late binding?

783


What is default class in c#?

657


What is a di class?

735


How long does it take to learn c# for unity?

668


What is short in c#?

727


what is the equivalent to regsvr32 and regsvr32 /u a file in .net development?

738


What is addressof operator?

712


What do you mean by casting a data type?

698