C#advanceddotnet

Spans, ranges, and indices

Slice arrays, strings, and stack memory by position — and do it without allocating a single copy.

^ indexes from the end, .. carves a range, and Span<T> gives you a typed window over existing memory that never copies.

Index from the end with ^

C#intro
0ms
alicedavecaroldave^0 is out of range
Reference · output compiled offline, not runnable in-browser

queue[^1] and queue[^2] count back from the end to print dave and carol, and storing ^1 in an Index last reuses that end-relative offset. ^0 resolves to queue.Length, which is one past the final element, so indexing it throws IndexOutOfRangeException — confirming ^ is plain arithmetic, not a "safe" wrap-around.