Tiny, fast ref struct string builder.
Backed by ArrayPool<char>. Low allocations. Short-lived use.
dotnet add package Soenneker.Utils.PooledStringBuildersusing Soenneker.Utils.PooledStringBuilders;
using var sb = new PooledStringBuilder(128);
sb.Append("Hello, ");
sb.Append(name);
sb.Append(' ');
sb.Append(id); // ISpanFormattable path, no boxing
sb.AppendLine();
string s = sb.ToString(); // returns string + returns buffernew PooledStringBuilder(int capacity = 128)Append(char),Append(string?),Append(ReadOnlySpan<char>)Append<T>(T value, ReadOnlySpan<char> fmt = default, IFormatProvider? prov = null)whereT : ISpanFormattableAppendSpan(int length)→ write directly into the bufferAppendLine(),AppendSeparatorIfNotEmpty(char)Length,Capacity,Clear()ToString()(keep using; you mustDispose()later)ToStringAndDispose(bool clear = false)(one-shot finish)Dispose()/Dispose(bool clear)
ref struct→ stack-only. Don’t capture, box, store in fields, or crossawait.- Dispose when done.
usingshould be used, or there isToStringAndDispose(). Don't use both. - Handling secrets? Use
ToStringAndDispose(clear: true)to zero the array before returning to the pool. - Not thread-safe. Keep it short-lived and single-scope.
