Span<T> and Memory<T>
Context: Span<T> and Memory<T> are ref‑struct types introduced in .NET Core 2.1 to provide safe, allocation‑free access to contiguous memory regions. They can represent arrays, strings, or native buffers. Span<T> is stack‑only, while Memory<T> can live on the heap and be used in async methods. They are critical for high‑performance scenarios where you want to avoid copying data.
Usage Example
Section titled “Usage Example”using System;
public class SpanDemo{ public static void SliceExample() { int[] numbers = { 1, 2, 3, 4, 5 }; Span<int> span = numbers.AsSpan(); Span<int> slice = span.Slice(1, 3); // {2,3,4} slice[0] = 10; // Modifies original array Console.WriteLine(numbers[1]); // 10 }}Output console
Section titled “Output console”dotnet run10Important notes
Section titled “Important notes”Span<T>cannot be used as a field in async methods or classes.Memory<T>is a heap‑friendly alternative for async scenarios.
Real‑world usage example
Section titled “Real‑world usage example”Parsing CSV lines – Instead of allocating substrings, use Span<char> and Slice to parse comma‑separated values without allocations.
See .NET docs on Span