Span<T> Visualizer
Understand memory slices, zero-allocation operations, and the Stack vs Heap
Span<T> (introduced in .NET Core 2.1) represents a contiguous slice of memory. You can think of it as a very thin wrapper holding just two things: a pointer to the start of the memory and a length.
It is a readonly ref struct, which means it always lives on the Stack - never on the Heap. This makes allocation essentially free. The trade-off: you can't store it as a field on a class. Stack vs Heap deep-dive ↗
ReadOnlySpan<T> is the same concept but does not allow mutating the memory it points to - perfect for reading strings without copying them.
Configure
Max 20 characters
Span type
Span<char> struct
Read-write: can mutate memory in-place
C# code
string s = "Hello World";
Span<char> span =
s.AsSpan(0, 5); Underlying char[] - @ 0x5000
Span<char> window: indices [0..4] - "Hello"
No copy made. The span just knows: start at 0x5000 and read 5 chars.
💡 Key insight
Span<char> is just a struct containing a pointer and a length. Moving the window start/length sliders above does not copy any characters - it only changes two numbers in the struct.
