---
source: https://iinventedlight.com/library/notes/csharp-span-memory.md
title: "Span<T> and Memory<T> in .NET"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (dotnet/docs); prose restates cited source lines"
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: C#"
tags: ['csharp', 'dotnet', 'span', 'performance']
sources:
  - id: S1
    title: "Memory-related and span types - .NET"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/standard/memory-and-spans/
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
  - id: S2
    title: "Memory<T> and Span<T> usage guidelines - .NET"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/standard/memory-and-spans/memory-t-usage-guidelines
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
---
# 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.

```csharp
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
- 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).
