Caffeinated Coder
Book an appointment
Lesson 20 · Advanced .NET

Span<T> and performance

Copying data costs time, and every new heap object is work for the garbage collector. A Span<T> is a view of memory that already exists: a reference to the first element plus a length. Follow a nine-line program that adds up the numbers in "12,34,5" with a ReadOnlySpan<char>: slicing moves the view, never the chars, so the parsing makes no new strings and the program prints 51, where csv.Split(',') would make 4 new heap objects.

14 min9 steps5 questionsAdvanced
Fig 20 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.