<p>just realised you can do this in C#</p><p>const int MaxStackAlloc = 1024;<br />Span<int> indices = count <= MaxStackAlloc ? stackalloc int[count] : new int[count];</p><p>so you dynamically get stack or heap allocation depending on the count.</p>