Skip to content

ListPool

Provides a shared pool of List<T> instances to reduce allocations.

C# Signature
internal static class ListPool<T>

The following example gets a list from the pool, fills it, and returns it when done.

C#
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);
}
}
}
}

Retrieves a List<T> from the pool.

C# Signature
public static List<T> Get();

A List<T> from the pool.

Retrieves a List<T> from the pool and wraps it in a disposable scope that returns it automatically.

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

When this method returns, contains the List<T> retrieved from the pool.

A disposable scope that returns the List<T> to the pool when disposed.

Returns a List<T> to the pool.

C# Signature
public static void Return(List<T> item);

The List<T> to return to the pool.