← All notes

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)

  1. For a synchronous API, take Span<T> rather than Memory<T> if possible.
  2. Use ReadOnlySpan<T> or ReadOnlyMemory<T> when the buffer is read-only.
  3. If a method takes Memory<T> and returns void, do not use it after returning.
  4. If it returns a Task, do not use the buffer after the task completes.

Claims

Sources

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

Permanent ID: iil:note:hsf68dq2

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.},
}

Markdown

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.