What is the difference between StringBuilder and String in C# ?
StringBuilder is immutable where as String is mutable.
Objects of type StringBuilder are mutable where as objects of type System.String are immutable.
String is more efficient than the StringBuilder for string manipulation.
All the above
StringBuilder is mutable and string is immutable .
Let us try to understand what is immutability .
Explanation: you declare a string s1="skill";
When clr executes the above given code variable s1 is created in memory which is having address of the location where string "skill" is stored ( all strings are stored in String buffer or string pool which is created in ram memory ).
The speciality of the string buffer is once data assigned in that memory no changes are allowed .
Later you try to modify content of a string using s1=s1+"gun";
When clr executes above piece of code the new string "skillgun" is created in new memory location of string buffer and the new memory address is assigned into string variable s1 .
You may get question here about the previous memory which was storing "skill" answer is the word "skill" is still present in the same memory location where it was allocated but no variable will be pointing to that memory or the previous memory location which contains word "skill" got inaccessible .
Back To Top