Friday, March 7, 2014

Difference between String and Stringbuilder in Asp.net - c#



Here I will happy to explain what is difference between string and stringbuilder in asp.net using C#.



String
StringBuilder
It’s an immutable(means once we create   string object we cannot modify) It’s mutable(means once we create string builder object we can perform any operation like insert, replace or append)
In string we do not have append keyword In StringBuilder we can use append keyword
String is slow performance wise because every time it will create new instance stringbuilder is high performance wise because it will use same instance of object to perform action like insert, replace or append
String belongs to System namespace Stringbuilder belongs to System.Text namespace
Example
string strTest = "hello";
strTest += "wow"; // create a new string instance
strTest += "nice"; // create a new string instance
Example
StringBuilder sb = new StringBuilder("hello");
sb.Append("wow");
sb.Append("nice");
string strTest = sb.ToString();


No comments:

Post a Comment