⚡ Zero-Allocation Slicing
Span<T> gives a view into memory without copying. No allocations, no GC pressure. Ideal for parsing, string manipulation, and high-performance scenarios.
❌ Allocating Substring
// Allocates new string (GC pressure) string data = "123,456,789"; string first = data.Substring(0, 3); // New string allocation string second = data.Substring(4, 3); // Another allocation // 1000 operations = 1000 allocations, GC collects constantly
✅ Span View (No Allocation)
ReadOnlySpan<char> data = "123,456,789"; ReadOnlySpan<char> first = data.Slice(0, 3); // View, no allocation ReadOnlySpan<char> second = data.Slice(4, 3); // View, no allocation // 1000 operations = 0 allocations, zero GC pressure
🎯 Practical Examples
// Parse CSV line without allocations
ReadOnlySpan<char> line = "John,Doe,30,Engineer";
int start = 0;
for (int i = 0; i < line.Length; i++)
{
if (line[i] == ',')
{
var field = line.Slice(start, i - start);
ProcessField(field); // No string allocation!
start = i + 1;
}
}
// Fast number parsing from span
ReadOnlySpan<char> number = "12345";
if (int.TryParse(number, out int value))
{
Console.WriteLine(value); // No temporary string
}
// Working with byte arrays
byte[] buffer = new byte[1024];
Span<byte> span = buffer.AsSpan();
span[0] = 0xFF;
span[1] = 0xAA;
// Stack allocation (ultra-fast, no heap)
Span<int> numbers = stackalloc int[100];
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = i * 2;
}
✅ Real-World Performance Gain
// Without Span (allocates 1,000,000 strings)
string data = GetLargeString();
for (int i = 0; i < 1000000; i++)
{
string part = data.Substring(i, 10);
Process(part); // 1M allocations, GC thrashing
}
// Execution time: 5 seconds, GC collects every second
// With Span (zero allocations)
ReadOnlySpan<char> data = GetLargeStringSpan();
for (int i = 0; i < 1000000; i++)
{
var part = data.Slice(i, 10);
Process(part); // 0 allocations
}
// Execution time: 0.3 seconds, no GC during processing
📝 Span-Compatible APIs
// String extensions
ReadOnlySpan<char> text = "Hello World";
bool hasSpace = text.Contains(' ');
int index = text.IndexOf('W');
// Memory extensions
Memory<byte> memory = new byte[1024];
Span<byte> span = memory.Span;
// List to span (without copy)
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };
Span<int> spanFromList = CollectionsMarshal.AsSpan(numbers);
// String to span (no allocation)
ReadOnlySpan<char> spanFromString = "Hello";
// Utf8 parsing (high performance)
ReadOnlySpan<byte> utf8 = Encoding.UTF8.GetBytes("Hello");
var parsed = Encoding.UTF8.GetString(utf8);
💡 When to Use Span
- High-frequency parsing (logs, CSV, JSON chunks)
- Network packet processing
- String manipulation in tight loops
- Working with stackalloc for temporary buffers
- Interop with native code (Span<T> pins memory automatically)
- Don’t use: For long-lived storage (Span is ref-struct, can’t be field)
“Parsing 10GB log file. Substring allocated millions of strings → GC paused every second. Rewrote with ReadOnlySpan. Zero allocations. Processing time: 45 minutes → 4 minutes.”



