difference between System.String and System.StringBuilder

Posted by Saranya Boopathi under C# on 6/26/2013 | Points: 10 | Views : 2620 | Status : [Member] | Replies : 1
Hi,
What's the difference between System.String and System.StringBuilder classes?

Saranya Boopathi


Responses

Posted by: aswinialuri-19361 on: 6/26/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Saranya,
System.string:
system.string is immutable, where as if string is immutable once we created we cannot modified,Suppose if we want give new value to old value simply it will discarded the old value and it will create new instance in memory to hold the new value.
Example:
String str = "ABC";

str.Replace("A","X");

Here Replace() method will not change data that "str" contains, instead a new string object is created to hold data "XBC" and the reference to this object is returned by Replace() method.

So in order to point str to this object we need to write below line.
str = str.Replace("A","X");
Now the new object is assigned to the variable str. earlier object that was assigned to str will take care by garbage collector as this one is no longer in used.

system.stringbuilder:
system.string builder is mutable append keyword is used in string builder but it doesn't used in system.string.


Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Saranya Boopathi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response