Project · Memory
A bounded buffer lease report
Return pooled storage once while keeping only an independent report.
All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.
Program.cs
Console.WriteLine(BufferReport.Make(8));
try { BufferReport.Make(-1); } catch (ArgumentOutOfRangeException) { Console.WriteLine("invalid:rejected"); }
BufferReport.cs
using System.Buffers;
public static class BufferReport { public static string Make(int length) { if (length < 1 || length > 4096) throw new ArgumentOutOfRangeException(nameof(length)); var b = ArrayPool<byte>.Shared.Rent(length); try { b.AsSpan(0,length).Clear(); b[0]=42; return $"logical:{length};first:{b[0]}"; } finally { ArrayPool<byte>.Shared.Return(b,clearArray:true); } } }