The following are the difference between string and StringBuilder
1. String is Immutable which will create new string object from System. String class in memory.
While String Builder is mutable (Fixed) so it will not allocated in memory.
2.If we have to do much string insertion manipulation then we have to use string builder. It is mutable. It will save the memory. But string is immutable so it will create a new memory every time.
For example
StringBuilder returnNumber = new StringBuilder(10000);
for(int i = 0; i<1000; i++)
{
returnNumber.Append(i.ToString());
}
But if we have to do very small work like simple concatenation then we have to use string.
String a=”test1”;
String b =”test2”;
String c=a+b;
Summary:
String Type:
1.It has immutable buffers.
2.It Cannot be changed.
3.Each operation returns a new string.
4.Copies can increase memory pressure.
StringBuilder type:
1.It has mutable buffers.
2.It Can be changed without copying.
3.It will be helpful in long loop condition.