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
TThe 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
ptrvoid*The pointer to the first element of the native memory
lengthintThe number of elements in the native memory
Exceptions
- ArgumentOutOfRangeException
lengthis negative
ReadOnlyMemory(T[])
Creates a memory over the entirety of an array. A null array gives an empty memory.
public ReadOnlyMemory(T[] array)
Parameters
arrayT[]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
arrayT[]The array to wrap
startintThe zero-based index of the first element to include
lengthintThe number of elements to include
Exceptions
- ArgumentOutOfRangeException
startorlengthare outside the bounds ofarray
Properties
Empty
Gets an empty ReadOnlyMemory<T>.
public static ReadOnlyMemory<T> Empty { get; }
Property Value
IsEmpty
Gets a value indicating whether this memory is empty.
public bool IsEmpty { get; }
Property Value
IsNative
Gets a value indicating whether this memory is backed by native memory.
public bool IsNative { get; }
Property Value
Length
Gets the number of elements in the memory region.
public int Length { get; }
Property Value
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
destinationT[]The array that receives the elements
destinationIndexintThe zero-based index in
destinationat which copying begins
Exceptions
- ArgumentNullException
destinationis null- ArgumentOutOfRangeException
destinationIndexis outside the bounds ofdestination- ArgumentException
destinationis 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
startintThe zero-based index at which the slice begins
Returns
- ReadOnlyMemory<T>
A memory containing the elements from
startto the end
Exceptions
- ArgumentOutOfRangeException
startis 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
startintThe zero-based index at which the slice begins
lengthintThe number of elements in the slice
Returns
- ReadOnlyMemory<T>
A memory containing
lengthelements starting atstart
Exceptions
- ArgumentOutOfRangeException
startorlengthare 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
segmentArraySegment<T>When this method returns true, the array segment backing this memory
Returns
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
segmentArraySegment<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
arrayT[]The array to convert
Returns
- ReadOnlyMemory<T>
A ReadOnlyMemory<T> over the entire array