Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Runtime.​InteropServices.​MemoryMarshal
Assembly: System.Runtime
Inheritance: object → MemoryMarshal
Provides methods to interoperate with <see cref="T:System.Memory`1" /> , <see cref="T:System.ReadOnlyMemory`1" /> , <see cref="T:System.Span`1" /> , and <see cref="T:System.ReadOnlySpan`1" /> .
Methods
public static
ReadOnlySpan<​byte>
AsBytes​(ReadOnlySpan<​T> span)
Casts a <see cref="T:System.ReadOnlySpan`1" /> of one primitive type, <paramref name="T" /> , to a <see langword="ReadOnlySpan<Byte>" /> .
Returns A read-only span of type <see cref="T:System.Byte" /> .
span
The source slice to convert.
public static
Span<​byte>
AsBytes​(Span<​T> span)
Casts a <see cref="T:System.Span`1" /> of one primitive type, <paramref name="T" /> , to a <see langword="Span<Byte>" /> .
Returns A span of type <see cref="T:System.Byte" /> .
span
The source slice to convert.
public static
Memory<​T>
AsMemory​(ReadOnlyMemory<​T> memory)
Creates a <see cref="T:System.Memory`1" /> instance from a <see cref="T:System.ReadOnlyMemory`1" /> .
Returns A memory block that represents the same memory as the <see cref="T:System.ReadOnlyMemory`1" /> .
memory
The read-only memory buffer.
public static
T&
AsRef​(ReadOnlySpan<​byte> span)
Reinterprets a read-only span of bytes as a read-only reference to the structure of type <paramref name="T" /> .
Returns The read-only reference to the structure of type <paramref name="T" /> .
span
The read-only span to reinterpret.
public static
T&
AsRef​(Span<​byte> span)
Reinterprets a span of bytes as a reference to the structure of type <paramref name="T" /> .
Returns The reference to the structure of type <paramref name="T" /> .
span
The span to reinterpret.
public static
ReadOnlySpan<​TTo>
Cast​(ReadOnlySpan<​TFrom> span)
Casts a read-only span of one primitive type to a read-only span of another primitive type.
Returns The converted read-only span.
span
The source slice to convert.
public static
Span<​TTo>
Cast​(Span<​TFrom> span)
Casts a span of one primitive type to a span of another primitive type.
Returns The converted span.
span
The source slice to convert.
public static
Memory<​T>
CreateFromPinnedArray​(T[] array,
int start,
int length)
Creates a new memory buffer over the portion of the pre-pinned target array beginning at the <paramref name="start" /> index and consisting of <paramref name="length" /> items.
Returns A block of memory over the specified elements of <paramref name="array" /> . If <paramref name="array" /> is <see langword="null" /> , or if <paramref name="start" /> and <paramref name="length" /> are 0, the method returns a <see cref="T:System.Memory`1" /> instance of <see cref="P:System.Memory`1.Length" /> zero.
array
The pre-pinned source array.
start
The index of <paramref name="array" /> at which to begin the memory block.
length
The number of items to include in the memory block.
public static
ReadOnlySpan<​byte>
CreateReadOnlySpanFromNullTerminated​(Byte* value)
Creates a new read-only span for a null-terminated sequence of bytes.
Returns A read-only span representing the specified sequence of bytes, or an empty span if the pointer is <see langword="null" /> .
value
The pointer to the null-terminated sequence of bytes.
public static
ReadOnlySpan<​char>
CreateReadOnlySpanFromNullTerminated​(Char* value)
Creates a new read-only span for a null-terminated string.
Returns A read-only span representing the specified null-terminated string, or an empty span if the pointer is <see langword="null" /> .
value
The pointer to the null-terminated string of characters.
public static
ReadOnlySpan<​T>
CreateReadOnlySpan​(T& reference,
int length)
public static
Byte&
GetArrayDataReference​(Array array)
Returns a reference to the 0th element of <paramref name="array" /> . If the array is empty, returns a reference to where the 0th element would have been stored. Such a reference may be used for pinning but must never be dereferenced.
Returns A reference to the 0th element of <paramref name="array" /> .
array
The array to analyze.
public static
T&
GetArrayDataReference​(T[] array)
Returns a reference to the 0th element of <paramref name="array" /> . If the array is empty, returns a reference to where the 0th element would have been stored. Such a reference may be used for pinning but must never be dereferenced.
Returns Reference to the 0th element in <paramref name="array" /> .
array
The array to analyze.
public static
T&
GetReference​(ReadOnlySpan<​T> span)
Returns a reference to the element of the read-only span at index 0.
Returns A reference to the element at index 0.
span
The read-only span from which the reference is retrieved.
public static
T&
GetReference​(Span<​T> span)
Returns a reference to the element of the span at index 0.
Returns A reference to the element at index 0.
span
The span from which the reference is retrieved.
public static
T
Read​(ReadOnlySpan<​byte> source)
Reads a structure of type <typeparamref name="T" /> out of a read-only span of bytes.
Returns The structure retrieved from the read-only span.
source
A read-only span.
public static
Collections.​Generic.​IEnumerable<​T>
ToEnumerable​(ReadOnlyMemory<​T> memory)
Creates an <see cref="T:System.Collections.Generic.IEnumerable`1" /> view of the given read-only memory buffer.
Returns An enumerable view of <paramref name="memory" /> .
memory
A read-only memory buffer.
public static
bool
TryGetArray​(ReadOnlyMemory<​T> memory,
ArraySegment`1& segment)
public static
bool
TryGetMemoryManager​(ReadOnlyMemory<​T> memory,
TManager& manager)
public static
bool
TryGetMemoryManager​(ReadOnlyMemory<​T> memory,
TManager& manager,
Int32& start,
Int32& length)
public static
bool
TryGetString​(ReadOnlyMemory<​char> memory,
String&? text,
Int32& start,
Int32& length)
public
bool
Equals​(object obj)
Inherited from object
Determines whether the specified object is equal to the current object.
Returns <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" /> .
obj
The object to compare with the current object.
protected
void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public
int
GetHashCode​()
Inherited from object
Serves as the default hash function.
Returns A hash code for the current object.
public
Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected
object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns A shallow copy of the current <see cref="T:System.Object" /> .
public
string
ToString​()
Inherited from object
Returns a string that represents the current object.
Returns A string that represents the current object.