StringBuilder

Taking the same example with reversing strings, we can use string concatenation for doing this:

var text = "Reversing strings is easy";
var reversedString = string.Empty;

for (int i = 0; i < text.Length; i++)
{
    reversedString += text[text.Length - i - 1];
}
Console.WriteLine(reversedString);

Immutability

But this is very inneficient.

Every single time we append a new character, the compiler creates a new string and assigns to the text variable.

So efficiency here is very low. 😢

String Concatenation

Any change to a string the compiler will produce a new string and will put that in the original string.

But by using this class, then it will behave like we were expecting a regular string to behave.

To solve this, we can use the StringBuilder object. This class was created to solve the problem with the immutability of strings.

It has a method called append:

Efficient approach

It is recommended to use a class named StringBuilder if you want to change the value of a string.

var builder = new StringBuilder();
builder.Append("Hello");
Console.WriteLine(builder.ToString());

Here we add more text to the string and here is why it is more benefic to use in this way.

builder.Append(", ").Append("World").Append("!");
Console.Write(builder.ToString());

How StringBuilder works?

The StringBuilder doesn't create any new string, it only creates a single object in memory and builds strings in the most efficient way possible.

TIP

So, whenever you want to change the value of a string, you should consider using StringBuilder to achieve efficiency and perfomance in your code.

Here is the reversed example with StringBuilder:

var text = "Sometimes strings are inefficient";
var reversedString = new StringBuilder();

for (int i = 0; i < text.Length; i++)
{
    reversedString.Append(text[text.Length - i - 1]);
}
Console.WriteLine(reversedString.ToString());

TIP

You can think of the StringBuilder as a class which models a mutable implementation of a string.