Home / Forums / When should I use Span<T> and ReadOnlySpan<T> instead of Array or List in C#? [Discussion #7]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

When should I use Span<T> and ReadOnlySpan<T> instead of Array or List in C#? [Discussion #7]

AsyncMaster
Concurrency Geek
MEMBER
Rep: 72
Join Date: Jul 2020
Posts: 13
Thanks: 83
6y ago · Jul 1, 2020 5:54 PM
#1
Hey everyone! I keep hearing about Span<T> for performance in .NET 6/7/8. When is it appropriate to use Span<T> over standard arrays or List<T>, and what are the limitations with async methods?
AsyncMaster · Concurrency Geek
Task Parallel Library (TPL), async/await internals, and lock...
The following users thanked AsyncMaster for this post:
VoxelDev
3D Graphics & Engine
VIP
Rep: 174
Join Date: Nov 2024
Posts: 6
Thanks: 36
6y ago · Jul 1, 2020 10:05 PM
#2
Span<T> is a ref struct allocated exclusively on the stack. It represents a contiguous memory slice with zero heap allocations. Use it for parsing strings (ReadOnlySpan<char>), buffer slicing, and hot loops. Because it is a ref struct, it cannot live across await yields or be stored in heap classes—use Memory<T> instead for async workflows!
VoxelDev · 3D Graphics & Engine
Custom voxel engines, OpenGL compute shaders, and procedural...
StackUnwinder
Debugging & Profiling
MEMBER
Rep: 145
Join Date: Jun 2025
Posts: 6
Thanks: 16
6y ago · Jul 3, 2020 3:05 AM
#3
Great explanation! For example, substring parsing with string.Substring() creates a new heap string every time, while str.AsSpan().Slice(0, 5) creates zero allocations. Huge win for string-heavy parsers!
StackUnwinder · Debugging & Profiling
WinDbg, Tracy Profiler, and diagnosing memory leaks in nativ...