Array operations creating copies wastes memory. Span
Traditional Array Slicing:
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] slice = numbers.Skip(2).Take(5).ToArray();
// Creates new array, copies 5 elements
With Span
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Span slice = numbers.AsSpan(2, 5);
// No copy, just points to same memory!
// slice[0] is numbers[2]
Performance:
// Process 1 million element array var data = new int[1_000_000]; // Old way (creates copy): ProcessArray(data.Skip(100).Take(1000).ToArray()); // 2ms + allocation // Span way (no copy): ProcessSpan(data.AsSpan(100, 1000)); // 0.05ms, no allocation // 40x faster!
Essential for high-performance code!
