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

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 #3]

ImGuiFanatic
Graphics & ImGui
MEMBER
Rep: 159
Join Date: Aug 2021
Posts: 6
Thanks: 35
6y ago · Jul 19, 2020 11:18 AM
#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?
ImGuiFanatic · Graphics & ImGui
Dear ImGui is the best GUI library ever made. Change my mind...
The following users thanked ImGuiFanatic for this post:
BytePusher
Memory & Performance
MEMBER
Rep: 209
Join Date: Dec 2025
Posts: 13
Thanks: 32
6y ago · Jul 19, 2020 2:02 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!
BytePusher · Memory & Performance
Zero-allocation C# code using Span<T>, Memory<T>, and Unsafe...
LinQ_Lover
C# Developer
MEMBER
Rep: 313
Join Date: Jul 2020
Posts: 8
Thanks: 27
6y ago · Jul 20, 2020 8:02 PM
#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!
LinQ_Lover · C# Developer
Clean code, fluent LINQ expressions, and expressive C# patte...