Difference between String and Stringbuilder in Asp.net,C#

String
 

String is immutable, Immutable means if you create string object then you     cannot modify it and It always create new object of string type in memory.
 
Example
 

 string strValue = "Hello Friend";
 // create a new string instance instead of changing the old one
 strValue += "Are
You ";
 strValue += "Ready ?";
 
Stringbuilder
 

StringBuilder is mutable, means if create string builder object then you can perform any operation like insert, replace or append without creating new instance for every time.it will update string at one place in memory does not create new space in memory.

Example
 

 StringBuilder sbValue = new StringBuilder("");
 sbValue.Append("
Hello Friend");
 sbValue.Append("Are You
Ready ?");
 string strValue = sbValue.ToString();
 



String
StringBuilder
immutable
mutable
Performance wise string is slow because every time it will create new instance
Performance wise stringbuilder is high because it will use same instance of object to perform any action
don’t have append keywor
can use append keyword
belongs to System namespace
belongs to System.Text namespace


No comments:

Post a Comment