Skip to main content

Practice ยท 1 of 2

Hunt the hidden boxing

CHALLENGE
Difficulty: advanced+25 XP

Implement static long SumInterface(IEnumerable<int> values) (sums via foreach over the interface โ€” allocates an enumerator + boxes if you use a struct enumerator through IEnumerable) and static long SumSpan(int[] values) (sums via Span<int> โ€” zero allocations). The test proves the allocation gap: SumSpan must allocate 0 over 100 runs; SumInterface's cost is printed for comparison. Implement both, correctly, and the numbers teach the lesson.

Back to lesson: Practice: Performance drills