How to loop over two variables in C# without allocating an array
foreach over (ReadOnlySpan<T>)[a, b] allocates nothing on .NET 8 and later and works for reference types, which stackalloc cannot do. On .NET 10 the compiler backs it with the BCL's InlineArray2<T>, and the JIT can even stack-allocate a plain new[] { a, b } once the method reaches Tier 1. Measured on .NET 10.0.10 and .NET 11 RC 3.