Span<T> and Memory<T> in .NET
Answer: Span<T> is a stack-only view over contiguous memory (an array, a stack buffer, native memory) that lets you slice without copying.
ReadOnlySpan<char> line = "key=value";
int eq = line.IndexOf('=');
var key = line[..eq]; // no allocation
var val = line[(eq + 1)..];
The family
| Type | Notes | |---|---| | IMemoryOwner<T> | owns a block and controls its lifetime | | ArraySegment<T> | older wrapper over part of an array |
Usage rules (abridged)
- For a synchronous API, take
Span<T>rather thanMemory<T>if possible. - Use
ReadOnlySpan<T>orReadOnlyMemory<T>when the buffer is read-only. - If a method takes
Memory<T>and returns void, do not use it after returning. - If it returns a
Task, do not use the buffer after the task completes.
Claims
- id: C1 claim: ReadOnlySpan<T> is an immutable Span<T> and can be backed by a String. source: S1 evidence: "System.ReadOnlySpan<T>, an immutable version of the Span<T> structure."
- id: C2 claim: IMemoryOwner<T> controls a memory block's lifetime. source: S1 evidence: "which represents the owner of a block of memory and controls its lifetime management."
- id: C3 claim: For a synchronous API, use Span<T> instead of Memory<T>. source: S2 evidence: "Rule #1: For a synchronous API, use Span<T> instead of Memory<T> as a parameter if"
- id: C4 claim: Use read-only types if the buffer should be read-only. source: S2 evidence: "Rule #2: Use ReadOnlySpan<T> or ReadOnlyMemory<T> if the buffer should be read-only"
- id: C5 claim: A buffer has a single owner for its entire lifetime. source: S2 evidence: "A buffer has a single owner for its entire lifetime."
Sources
- [S1] Memory-related and span types - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/standard/memory-and-spans/ . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] Memory<T> and Span<T> usage guidelines - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/standard/memory-and-spans/memory-t-usage-guidelines . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Memory-related and span types - .NET, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), Memory<T> and Span<T> usage guidelines - .NET, CC BY 4.0 (dotnet/docs).
Cite this
iinventedlight. 2026. "Span<T> and Memory<T> in .NET." iinventedlight Library, iil:note:hsf68dq2, version of 2026-11-04. https://iinventedlight.com/library/notes/csharp-span-memory/. License: CC-BY-4.0.
@misc{iil_note_hsf68dq2,
author = {{iinventedlight}},
title = {{Span<T> and Memory<T> in .NET}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/csharp-span-memory/},
note = {Permanent ID iil:note:hsf68dq2. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/csharp-span-memory.md.