Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Span: practical reference

System.Span: practical reference: Span<T> is a stack-only mutable view of a contiguous memory region.

Version: .NET API; verified against .NET 10 reference surface

Syntax

System.Span /* select a documented member or constructor */

Span is a stack-only mutable view of a contiguous memory region.

Use it on synchronous hot paths to work without copying.

As a ref struct it cannot escape to the heap or cross await or yield.

Slice and indexing validate bounds; stackalloc has a limited stack budget.

The complete program actually uses System.Span; its exact expected stdout is 3,2,1.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

Span<int> span = stackalloc[] { 1, 2, 3 };
span.Reverse();
Console.WriteLine(string.Join(",", span.ToArray()));

Result

3,2,1

The complete program actually uses `System.Span`; its exact expected stdout is `3,2,1`.

Sources