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

ref struct Span<char>
_pointer→ 0x5000
_length5

Read-write: can mutate memory in-place

C# code

string s = "Hello World";
Span<char> span =
  s.AsSpan(0, 5);
HEAP

Underlying char[] - @ 0x5000

H
[0]
0x5000
e
[1]
0x5002
l
[2]
0x5004
l
[3]
0x5006
o
[4]
0x5008
[5]
0x500A
W
[6]
0x500C
o
[7]
0x500E
r
[8]
0x5010
l
[9]
0x5012
d
[10]
0x5014

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.