Skip to content

StringBuilderPool

Provides a shared pool of StringBuilder instances to reduce allocations.

C# Signature
internal static class StringBuilderPool

The following example gets a StringBuilder from the pool and returns it when done.

C#
using System.Collections.Generic;
using System.Text;
using Hertzole.SourceGen;
public static class TextJoiner
{
public static string Join(IEnumerable<string> values)
{
// The scope returns and clears the builder when disposed.
using (StringBuilderPool.Get(out StringBuilder builder))
{
foreach (string value in values)
{
builder.Append(value);
}
return builder.ToString();
}
}
}

Retrieves a StringBuilder from the pool.

C# Signature
public static StringBuilder Get();

A StringBuilder from the pool.

Retrieves a StringBuilder from the pool and wraps it in a disposable scope that returns it automatically.

C# Signature
public static PoolScope<StringBuilder> Get(out StringBuilder item);

When this method returns, contains the StringBuilder retrieved from the pool.

A disposable scope that returns the StringBuilder to the pool when disposed.

Returns a StringBuilder to the pool and clears the StringBuilder.

C# Signature
public static void Return(StringBuilder item);

The StringBuilder to return to the pool.