ZeroAlloc.Collections is a high-performance, zero-allocation collections library for .NET. It provides six collection types — each available as a ref struct for stack-only scenarios and as a heap-allocated class for use in async code, fields, and DI containers. All collections rent their backing storage from ArrayPool<T>.Shared, return it on Dispose(), and expose Span<T> accessors for tight inner loops. Source generators are included for emitting specialized, type-specific collection implementations at compile time.
dotnet add package ZeroAlloc.Collectionsusing ZeroAlloc.Collections;
using var list = new PooledList<int>(capacity: 64);
list.Add(1);
list.Add(2);
list.Add(3);
foreach (var item in list)
{
Console.WriteLine(item);
}The list rents from ArrayPool<T>.Shared on construction and returns the buffer when Dispose() runs — zero heap allocation for the backing array.
using ZeroAlloc.Collections;
using var ring = new RingBuffer<string>(capacity: 4);
ring.TryWrite("alpha");
ring.TryWrite("beta");
ring.TryWrite("gamma");
while (ring.TryRead(out var item))
{
Console.WriteLine(item); // alpha, beta, gamma
}A fixed-capacity circular buffer suitable for producer/consumer queues, telemetry windows, and bounded logging.
- Zero Allocation — all pooled collections rent from
ArrayPool<T>.Sharedand return buffers on disposal - Ref Struct Variants — stack-only types with compile-time lifetime enforcement and
ref Tindexers - Heap Variants — class-based counterparts that implement
IDisposable,IList<T>, andIReadOnlyList<T>for use in async methods and DI - Span Accessors —
AsSpan()andAsReadOnlySpan()on every collection for zero-copy interop - Source Generators — emit type-specific collections, pooled wrappers, and
ref structenumerators at compile time - Analyzer Diagnostics — build-time warnings for undisposed collections and accidental ref struct copies
- Multi-TFM — targets
netstandard2.1,net8.0, andnet9.0(allows ref structon .NET 9) - Native AOT Compatible — no reflection, no dynamic code generation
| Type | Ref Struct | Heap Variant | Description |
|---|---|---|---|
PooledList<T> |
Yes | HeapPooledList<T> |
Pooled-backed growable list |
RingBuffer<T> |
Yes | HeapRingBuffer<T> |
Fixed-capacity circular buffer |
SpanDictionary<TKey,TValue> |
Yes | HeapSpanDictionary<TKey,TValue> |
Open-addressing hash map |
PooledStack<T> |
Yes | HeapPooledStack<T> |
Pooled-backed LIFO stack |
PooledQueue<T> |
Yes | HeapPooledQueue<T> |
Pooled-backed FIFO queue |
FixedSizeList<T> |
Yes | HeapFixedSizeList<T> |
Stack-allocated fixed-capacity list |
| Page | Description |
|---|---|
| Getting Started | Install and use your first collection in five minutes |
| PooledList | Growable list backed by ArrayPool<T> |
| RingBuffer | Fixed-capacity circular buffer |
| SpanDictionary | Open-addressing hash map with Span accessors |
| PooledStack & PooledQueue | LIFO stack and FIFO queue with pooled storage |
| FixedSizeList | Stack-allocated fixed-capacity list |
| Source Generators | Emit type-specific collections at compile time |
| Diagnostics | Analyzer warnings and error reference |
| Performance | Benchmark results and zero-alloc design internals |
| Testing | Unit-test collections with xUnit |
MIT