best way to clear contents of .NET's StringBuilder
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The best way to clear a StringBuilder in .NET is usually to keep the instance and remove its current content instead of allocating a new one. In modern .NET, the clearest API is Clear(), and under the hood it is effectively the same idea as setting Length = 0.
The Recommended Approach
Use Clear() when your target framework supports it:
This is readable, direct, and communicates intent clearly.
Equivalent Older Approach
Before Clear() was added, the common technique was:
That still works and is effectively the same operation for most practical purposes. It clears the current content without discarding the internal capacity.
This reuse is exactly why StringBuilder exists in the first place.
Why Reuse Is Often Better Than Reallocation
If you repeatedly create a new StringBuilder inside a hot loop, you generate more allocations and more work for the garbage collector. Reusing an existing builder avoids that churn.
For example:
This pattern is common in logging, CSV generation, protocol formatting, and other repeated string-construction code.
What Clearing Does Not Do
Clearing the builder removes the characters, but it usually does not shrink the underlying buffer. That is often desirable because it makes future appends cheaper. If a builder once grew very large, though, retaining that capacity may waste memory if you no longer need it.
In that case, replacing the instance can make sense:
Or, if you know the expected future size, recreate it with a more reasonable capacity:
So the real answer is:
- use
Clear()orLength = 0for normal reuse - allocate a new builder only when you intentionally want to drop a large retained buffer
Capacity Matters
You can inspect and adjust capacity if memory usage is important:
Be careful with manual capacity changes. Most code does not need them, and premature tuning can make the code harder to read without any measurable gain.
API Readability Versus Compatibility
If your codebase targets a modern .NET version, Clear() is preferable because it reads like exactly what you mean. If you work in older frameworks or shared code with older APIs, Length = 0 remains perfectly valid.
The performance difference between those two styles is not the real issue. The more important question is whether you should reuse the same builder at all or replace it because of retained capacity.
Common Pitfalls
- Recreating a new
StringBuilderevery time when simple reuse would be cheaper. - Assuming
Clear()also shrinks memory usage back to a minimal size. - Manually tuning capacity without evidence that it matters.
- Keeping a giant builder alive after one unusually large operation.
- Forgetting that
StringBuilderis mutable and should not be shared unsafely across threads.
Summary
- Use
StringBuilder.Clear()as the clearest modern way to empty a builder. - '
builder.Length = 0is the older equivalent and still works.' - Clearing removes content but usually keeps the allocated capacity.
- Reuse the builder for repeated work unless you specifically want to release a large buffer.
- Choose readability first, then optimize capacity only when measurement justifies it.

