"String" is immutable. That is we can not change the value of string once assigned. If we assign the value to string inside loops then each time the object will be destroyed and created again.
"StringBuilder" is mutable. That is the object will not be recreated if there is any modification, instead it will append the new value with old value. Consider the following piece of code:
StringBuilder sbMutalble = new StringBuilder();
string sImmutable = "1";
for (int val = 1; val <= 9; val++)
sImmutable += val;
Here the string varaible "sImmutable" will be recreated for 10 times, because in each time we are adding a new value to the existing value.
In case of stringbuilder the varaible "sbMutable" will not be recreated instead it will append the new values
Asked In: CTS |