C-SHARP - 4.8 StringBuilder
This video introduces the StringBuilder class. A reminder first: a string in C# is an object of the String class and it is immutable. Every method we call on it returns a brand-new string in memory, which means each modification allocates fresh memory. When we perform many modifications in a loop, the overhead becomes significant.
To work around this, the .NET framework offers StringBuilder, which holds an internal mutable buffer. Operations such as concatenation, insertion and replacement happen inside the same object, avoiding repeated allocations. StringBuilder is optimised for editing but, unlike String, it does not expose search methods such as IndexOf or LastIndexOf.
using System.Text;
var builder = new StringBuilder();
builder.Append('*', 10);
builder.AppendLine();
builder.Append("Welcome");
builder.Replace('*', '-');
builder.Remove(0, 5);
builder.Insert(0, "+++");
Console.WriteLine(builder);
Most useful methods
Append/AppendLine: add content at the end of the buffer.Insert(index, value): insert content at a specific position.Remove(index, length): delete a portion of the buffer.Replace(oldValue, newValue): substitute a character or substring.Clear(): empty the entire builder.
You will use StringBuilder whenever you need to concatenate many fragments in a loop or build a long, dynamic output. For one-off transformations the String class is fine; for heavy editing the builder pays off in performance. The next video walks through these methods step by step on Visual Studio.
Summary
In this lesson on StringBuilder in C#, you'll learn why regular strings cause performance overhead due to immutability: every modification creates a new object in memory. StringBuilder solves this problem by allowing efficient string concatenation and manipulation without creating new objects each time. While StringBuilder sacrifices search optimization compared to the String class, it provides essential methods like Append, Insert, Remove, Replace, and Clear for building complex strings efficiently.
Key points
- Strings in C# are immutable objects—every modification creates a new object in memory, causing performance overhead
- StringBuilder eliminates memory waste by allowing in-place string modifications without creating new objects
- StringBuilder excels at concatenating large numbers of strings in loops but lacks search methods like IndexOf and LastIndexOf
- Core StringBuilder methods: Append (add to end), Insert (add at index), Remove (delete), Replace (swap), Clear (erase)
- StringBuilder is optimized for building strings; String class is optimized for searching and reading
- Use StringBuilder for heavy string manipulation tasks; use String when you need search capabilities
FAQ
Why does modifying a string create a new object in memory?
Strings in C# are immutable, meaning they cannot be changed once created. Any modification—such as concatenation or character replacement—requires creating an entirely new string object and allocating new memory space for it. This approach ensures string safety but incurs performance costs with repeated modifications.
When should I use StringBuilder instead of string concatenation?
Use StringBuilder when you need to perform multiple concatenations or modifications, especially in loops. StringBuilder maintains the string in memory and modifies it in place, avoiding the memory allocation overhead of creating dozens of temporary string objects. For single concatenations, regular string operations are sufficient.
What are the limitations of StringBuilder compared to String?
StringBuilder is not optimized for search operations. It lacks methods like IndexOf, LastIndexOf, and other search functionality that the String class provides. If your code requires frequent searching within a string, convert StringBuilder to String (via ToString()) before performing those operations.