Table of Contents

Struct ReadOnlyMemory<T>

Namespace
System
Assembly
C2JBridge.dll

Compatibility shim of ReadOnlyMemory<T> for .NET Framework targets.

Unlike the official type it can be backed either by a managed array or by a native pointer. A managed array is never exposed through a raw pointer unless pinned: use TryGetArray(out ArraySegment<T>) or Pin(). Span is available only for native-backed instances.

public readonly struct ReadOnlyMemory<T> where T : unmanaged

Type Parameters

T

The type of elements. Must be an unmanaged type.

Inherited Members
Extension Methods

Constructors

ReadOnlyMemory(void*, int)

Creates a memory over native memory. The caller guarantees that the memory stays valid and fixed for the whole lifetime of the instance and of any copy of it.

public ReadOnlyMemory(void* ptr, int length)

Parameters

ptr void*

The pointer to the first element of the native memory

length int

The number of elements in the native memory

Exceptions

ArgumentOutOfRangeException

length is negative

ReadOnlyMemory(T[])

Creates a memory over the entirety of an array. A null array gives an empty memory.

public ReadOnlyMemory(T[] array)

Parameters

array T[]

The array to wrap, or null for an empty memory

ReadOnlyMemory(T[], int, int)

Creates a memory over a portion of an array.

public ReadOnlyMemory(T[] array, int start, int length)

Parameters

array T[]

The array to wrap

start int

The zero-based index of the first element to include

length int

The number of elements to include

Exceptions

ArgumentOutOfRangeException

start or length are outside the bounds of array

Properties

Empty

Gets an empty ReadOnlyMemory<T>.

public static ReadOnlyMemory<T> Empty { get; }

Property Value

ReadOnlyMemory<T>

IsEmpty

Gets a value indicating whether this memory is empty.

public bool IsEmpty { get; }

Property Value

bool

IsNative

Gets a value indicating whether this memory is backed by native memory.

public bool IsNative { get; }

Property Value

bool

Length

Gets the number of elements in the memory region.

public int Length { get; }

Property Value

int

Span

Gets the span over this memory. Supported only for native-backed (or empty) instances: a managed array cannot be exposed through a raw pointer without pinning it.

public ReadOnlySpan<T> Span { get; }

Property Value

ReadOnlySpan<T>

Exceptions

InvalidOperationException

The memory is backed by a managed array.

Methods

CopyTo(T[], int)

Copies the content into a destination array.

public void CopyTo(T[] destination, int destinationIndex = 0)

Parameters

destination T[]

The array that receives the elements

destinationIndex int

The zero-based index in destination at which copying begins

Exceptions

ArgumentNullException

destination is null

ArgumentOutOfRangeException

destinationIndex is outside the bounds of destination

ArgumentException

destination is too small

Pin()

Pins the memory and returns a handle exposing a stable pointer to the first element. The handle must be disposed. For native-backed memory no pinning is needed.

public PinnedMemoryHandle Pin()

Returns

PinnedMemoryHandle

A PinnedMemoryHandle that must be disposed to release the pin

Slice(int)

Forms a slice starting at the specified index.

public ReadOnlyMemory<T> Slice(int start)

Parameters

start int

The zero-based index at which the slice begins

Returns

ReadOnlyMemory<T>

A memory containing the elements from start to the end

Exceptions

ArgumentOutOfRangeException

start is greater than Length

Slice(int, int)

Forms a slice of the specified length starting at the specified index.

public ReadOnlyMemory<T> Slice(int start, int length)

Parameters

start int

The zero-based index at which the slice begins

length int

The number of elements in the slice

Returns

ReadOnlyMemory<T>

A memory containing length elements starting at start

Exceptions

ArgumentOutOfRangeException

start or length are outside the bounds of this memory

ToArray()

Copies the content into a new array.

public T[] ToArray()

Returns

T[]

A new array containing a copy of the elements, or an empty array if this memory is empty

TryGetArray(out ArraySegment<T>)

Tries to get the underlying array segment. Returns false for native-backed memory.

public bool TryGetArray(out ArraySegment<T> segment)

Parameters

segment ArraySegment<T>

When this method returns true, the array segment backing this memory

Returns

bool

true if the memory is array-backed or empty; false if it is native-backed

Operators

implicit operator ReadOnlyMemory<T>(ArraySegment<T>)

Implicitly converts an ArraySegment<T> to a ReadOnlyMemory<T>.

public static implicit operator ReadOnlyMemory<T>(ArraySegment<T> segment)

Parameters

segment ArraySegment<T>

The array segment to convert

Returns

ReadOnlyMemory<T>

A ReadOnlyMemory<T> over the segment, or an empty memory if the segment has no array

implicit operator ReadOnlyMemory<T>(T[])

Implicitly converts an array to a ReadOnlyMemory<T>.

public static implicit operator ReadOnlyMemory<T>(T[] array)

Parameters

array T[]

The array to convert

Returns

ReadOnlyMemory<T>

A ReadOnlyMemory<T> over the entire array