In C#, the string data type is immutable. This means once a value is assigned to the variable, assigning a new value or doing any operation to the same variable creates a new memory location rather than using the same.
For example:
string myString = “My name is Khan”;
myString.ToUpper();
After running the above code, the myString variable will have still “My name is Khan”.
In C#, the StringBuilder is mutable. There are operations like Append, Remove, can be performed on a StringBuilder variable. The same memory location being used for operations. The string will grow or shrink from the same memory location.
If you want to concatenate few strings in your code, would you use string or stringbuilder? The rule of thumb you can often use is, if you are concatenating 5 or less strings, the string is okay. If it goes beyond 5, the stringbuilder is efficient.