Register now or log in to join your professional community.
When you add something to a string, a new address in the memory is being allocated. The previous string is copied to a new location with the newly added part.In case of StringBuilder which keeps the same position in the memory without performing the copy operation. That means string is inefficient.
Example:
string str = "hi";
// create a new string instance instead of changing the old one
str += "hai1";
str += "hai2";
//StringBuilder working
StringBuilder sb = new StringBuilder("");
sb.Append("ha1");
sb.Append("hai2 ");
string str = sb.ToString();