ListPool
Provides a shared pool of List<T> instances to reduce allocations.
internal static class ListPool<T>Examples
Section titled “Examples”The following example gets a list from the pool, fills it, and returns it when done.
using System.Collections.Generic;using Hertzole.SourceGen;
public static class Collector{ public static void Collect(IEnumerable<string> values) { // The scope returns the list to the pool when disposed. using (ListPool<string>.Get(out List<string> list)) { list.AddRange(values);
foreach (string value in list) { System.Console.WriteLine(value); } } }}Methods
Section titled “Methods”Retrieves a List<T> from the pool.
public static List<T> Get();Returns
Section titled “Returns”A List<T> from the pool.
Get(out List<T>)
Section titled “Get(out List<T>)”Retrieves a List<T> from the pool and wraps it in a disposable scope that returns it automatically.
public static PoolScope<List<T>> Get(out List<T> item);When this method returns, contains the List<T> retrieved from the pool.
Returns
Section titled “Returns”A disposable scope that returns the List<T> to the pool when disposed.
Return(List<T>)
Section titled “Return(List<T>)”Returns a List<T> to the pool.
public static void Return(List<T> item);The List<T> to return to the pool.