DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="0.14.0" />

 ArrayRental<T>

public struct ArrayRental<T> : IDisposable
Represents array obtained from array pool.
using System; using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace DotNext.Buffers { public readonly struct ArrayRental<T> : IDisposable { private readonly ArrayPool<T> pool; private readonly T[] array; private readonly bool clearArray; public Memory<T> Memory { get { if (array != null) return new Memory<T>(array); return default(Memory<T>); } } public ArrayRental(ArrayPool<T> pool, int minimumLength, bool clearArray = false) { this.pool = pool; array = pool.Rent(minimumLength); this.clearArray = clearArray; } public ArrayRental(int minimumLength, bool clearArray = false) { this = new ArrayRental<T>(ArrayPool<T>.Shared, minimumLength, clearArray); } public static implicit operator T[]([In] [System.Runtime.CompilerServices.IsReadOnly] ref ArrayRental<T> rental) { return rental.array; } public void Dispose() { pool?.Return(array, clearArray); } } }