<PackageReference Include="System.Memory" Version="4.6.2" />

MemoryMarshal

public static class MemoryMarshal
Provides a collection of methods for interoperating with Memory<T>, ReadOnlyMemory<T>, Span<T>, and ReadOnlySpan<T>.
public static Span<byte> AsBytes<T>(Span<T> span) where T : struct

Casts a Span of one primitive type T to Span of bytes. That type may not contain pointers or references. This is checked at runtime in order to preserve type safety.

public static ReadOnlySpan<byte> AsBytes<T>(ReadOnlySpan<T> span) where T : struct

Casts a ReadOnlySpan of one primitive type T to ReadOnlySpan of bytes. That type may not contain pointers or references. This is checked at runtime in order to preserve type safety.

public static Memory<T> AsMemory<T>(ReadOnlyMemory<T> memory)

Creates a Memory<T> from a ReadOnlyMemory<T>.

public static Span<TTo> Cast<TFrom, TTo>(Span<TFrom> span) where TFrom : struct where TTo : struct

Casts a Span of one primitive type TFrom to another primitive type TTo. These types may not contain pointers or references. This is checked at runtime in order to preserve type safety.

public static ReadOnlySpan<TTo> Cast<TFrom, TTo>(ReadOnlySpan<TFrom> span) where TFrom : struct where TTo : struct

Casts a ReadOnlySpan of one primitive type TFrom to another primitive type TTo. These types may not contain pointers or references. This is checked at runtime in order to preserve type safety.

public static Memory<T> CreateFromPinnedArray<T>(T[] array, int start, int length)

Creates a new memory over the portion of the pre-pinned target array beginning at 'start' index and ending at 'end' index (exclusive).

public static ref T GetReference<T>(Span<T> span)

Returns a reference to the 0th element of the Span. If the Span is empty, returns a reference to the location where the 0th element would have been stored. Such a reference can be used for pinning but must never be dereferenced.

public static ref T GetReference<T>(ReadOnlySpan<T> span)

Returns a reference to the 0th element of the ReadOnlySpan. If the Span is empty, returns a reference to the location where the 0th element would have been stored. Such a reference can be used for pinning but must never be dereferenced.

public static T Read<T>(ReadOnlySpan<byte> source) where T : struct

Reads a structure of type T out of a read-only span of bytes.

public static IEnumerable<T> ToEnumerable<T>(ReadOnlyMemory<T> memory)

Creates an IEnumerable<T> view of the given memory to allow the memory to be used in existing APIs that take an IEnumerable<T>.

public static bool TryGetArray<T>(ReadOnlyMemory<T> memory, out ArraySegment segment)

Get an array segment from the underlying memory. If unable to get the array segment, return false with a default array segment.

public static bool TryGetMemoryManager<T, TManager>(ReadOnlyMemory<T> memory, out TManager manager) where TManager : MemoryManager<T>

Gets an MemoryManager<T> from the underlying read-only memory. If unable to get the TManager type, returns false.

public static bool TryGetMemoryManager<T, TManager>(ReadOnlyMemory<T> memory, out TManager manager, out int start, out int length) where TManager : MemoryManager<T>

Gets an MemoryManager<T> and start, length from the underlying read-only memory. If unable to get the TManager type, returns false.

public static bool TryGetString(ReadOnlyMemory<char> memory, out string text, out int start, out int length)

Attempts to get the underlying String from a ReadOnlyMemory<T>.

public static bool TryRead<T>(ReadOnlySpan<byte> source, out T value) where T : struct

Reads a structure of type T out of a span of bytes. If the span is too small to contain the type T, return false.

public static bool TryWrite<T>(Span<byte> destination, ref T value) where T : struct

Writes a structure of type T into a span of bytes. If the span is too small to contain the type T, return false.

public static void Write<T>(Span<byte> destination, ref T value) where T : struct

Writes a structure of type T into a span of bytes.