StringBuilderPool
Provides a shared pool of StringBuilder instances to reduce allocations.
internal static class StringBuilderPoolExamples
Section titled “Examples”The following example gets a StringBuilder from the pool and returns it when done.
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(); } }}Methods
Section titled “Methods”Retrieves a StringBuilder from the pool.
public static StringBuilder Get();Returns
Section titled “Returns”A StringBuilder from the pool.
Get(out StringBuilder)
Section titled “Get(out StringBuilder)”Retrieves a StringBuilder from the pool and wraps it in a disposable scope that returns it automatically.
public static PoolScope<StringBuilder> Get(out StringBuilder item);When this method returns, contains the StringBuilder retrieved from the pool.
Returns
Section titled “Returns”A disposable scope that returns the StringBuilder to the pool when disposed.
Return(StringBuilder)
Section titled “Return(StringBuilder)”Returns a StringBuilder to the pool and clears the StringBuilder.
public static void Return(StringBuilder item);The StringBuilder to return to the pool.