Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Memory: practical reference

System.Memory: practical reference: Memory<T> describes a contiguous memory region that can outlive the stack and cross an async boundary.

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

Syntax

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

Memory describes a contiguous memory region that can outlive the stack and cross an async boundary.

Use it to pass slices of arrays or owner-managed memory without copying.

The buffer remains owned by its owner; Memory does not release the resource.

Slice validates its range, and access after the owner releases the memory is an error.

The complete program actually uses System.Memory; its exact expected stdout is 9.

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;

Memory<int> memory = new[] { 1, 2, 3 };
memory.Span[1] = 9;
Console.WriteLine(memory.Span[1]);

Result

9

The complete program actually uses `System.Memory`; its exact expected stdout is `9`.

Sources