StackPool
Provides a shared pool of Stack<T> instances to reduce allocations.
internal static class StackPool<T>Examples
Section titled “Examples”The following example uses a pooled stack to process values in reverse order.
using System.Collections.Generic;using Hertzole.SourceGen;
public static class ReverseProcessor{ public static void Process(IEnumerable<int> values) { // The scope returns the stack to the pool when disposed. using (StackPool<int>.Get(out Stack<int> stack)) { foreach (int value in values) { stack.Push(value); }
while (stack.Count > 0) { System.Console.WriteLine(stack.Pop()); } } }}Methods
Section titled “Methods”Retrieves a Stack<T> from the pool.
public static Stack<T> Get();Returns
Section titled “Returns”A Stack<T> from the pool.
Get(out Stack<T>)
Section titled “Get(out Stack<T>)”Retrieves a Stack<T> from the pool and wraps it in a disposable scope that returns it automatically.
public static PoolScope<Stack<T>> Get(out Stack<T> item);When this method returns, contains the Stack<T> retrieved from the pool.
Returns
Section titled “Returns”A disposable scope that returns the Stack<T> to the pool when disposed.
Return(Stack<T>)
Section titled “Return(Stack<T>)”Returns a Stack<T> to the pool.
public static void Return(Stack<T> item);The Stack<T> to return to the pool.