Struct UnmanagedSpan<T>
UnmanagedSpan represents a contiguous region of arbitrary memory. Unlike arrays, it can point to either managed or native memory, or to memory allocated on the stack. It is type-safe and memory-safe.
public readonly ref struct UnmanagedSpan<T> where T : unmanaged
Type Parameters
T
- Inherited Members
- Extension Methods
Constructors
UnmanagedSpan(void*, long)
Creates a new span over the target unmanaged buffer. Clearly this is quite dangerous, because we are creating arbitrarily typed T's out of a void*-typed block of memory. And the length is not checked. But if this creation is correct, then all subsequent uses are correct.
[CLSCompliant(false)]
public UnmanagedSpan(void* pointer, long length)
Parameters
pointervoid*An unmanaged pointer to memory.
lengthlongThe number of
Telements the memory contains.
Exceptions
- ArgumentException
Thrown when
Tis reference type or contains pointers and hence cannot be stored in unmanaged memory.- ArgumentOutOfRangeException
Thrown when the specified
lengthis negative.
UnmanagedSpan(ref T)
Creates a new UnmanagedSpan<T> of length 1 around the specified reference.
public UnmanagedSpan(ref T reference)
Parameters
referenceTA reference to data.
UnmanagedSpan(T[]?)
Creates a new span over the entirety of the target array.
public UnmanagedSpan(T[]? array)
Parameters
arrayT[]The target array.
Remarks
Returns default when array is null.
Exceptions
- ArrayTypeMismatchException
Thrown when
arrayis covariant and array's type is not exactly T[].
UnmanagedSpan(T[]?, int, int)
Creates a new span over the portion of the target array beginning at 'start' index and ending at 'end' index (exclusive).
public UnmanagedSpan(T[]? array, int start, int length)
Parameters
arrayT[]The target array.
startintThe zero-based index at which to begin the span.
lengthintThe number of items in the span.
Remarks
Returns default when array is null.
Exceptions
- ArrayTypeMismatchException
Thrown when
arrayis covariant and array's type is not exactly T[].- ArgumentOutOfRangeException
Thrown when the specified
startor end index is not in the range (<0 or >Length).
Properties
Empty
Returns an empty UnmanagedSpan<T>
public static UnmanagedSpan<T> Empty { get; }
Property Value
IsEmpty
Gets a value indicating whether this UnmanagedSpan<T> is empty.
public bool IsEmpty { get; }
Property Value
this[long]
Returns a reference to specified element of the UnmanagedSpan.
public ref T this[long index] { get; }
Parameters
indexlongThe zero-based index.
Property Value
- T
Exceptions
- IndexOutOfRangeException
Thrown when index less than 0 or index greater than or equal to Length
Length
The number of items in the span.
public long Length { get; }
Property Value
Methods
Clear()
Clears the contents of this span.
public void Clear()
CopyTo(UnmanagedSpan<T>)
Copies the contents of this span into destination span. If the source and destinations overlap, this method behaves as if the original values in a temporary location before the destination is overwritten.
public void CopyTo(UnmanagedSpan<T> destination)
Parameters
destinationUnmanagedSpan<T>The span to copy items into.
Exceptions
- ArgumentException
Thrown when the destination UnmanagedSpan is shorter than the source UnmanagedSpan.
Fill(T)
Fills the contents of this span with the given value.
public void Fill(T value)
Parameters
valueT
GetEnumerator()
Gets an enumerator for this span.
public UnmanagedSpan<T>.Enumerator GetEnumerator()
Returns
Slice(long)
Forms a slice out of the given span, beginning at 'start'.
public UnmanagedSpan<T> Slice(long start)
Parameters
startlongThe zero-based index at which to begin this slice.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when the specified
startindex is not in range (<0 or >Length).
Slice(long, long)
Forms a slice out of the given span, beginning at 'start', of given length
public UnmanagedSpan<T> Slice(long start, long length)
Parameters
startlongThe zero-based index at which to begin this slice.
lengthlongThe desired length for the slice (exclusive).
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when the specified
startor end index is not in range (<0 or >Length).
ToArray()
Copies the contents of this span into a new array. This heap allocates, so should generally be avoided, however it is sometimes necessary to bridge the gap with APIs written in terms of arrays.
public T[] ToArray()
Returns
- T[]
ToString()
For UnmanagedSpan<T>, returns a new instance of string that represents the characters pointed to by the span. Otherwise, returns a string with the name of the type and the number of elements.
public override string ToString()
Returns
TryCopyTo(UnmanagedSpan<T>)
Copies the contents of this span into destination span. If the source and destinations overlap, this method behaves as if the original values in a temporary location before the destination is overwritten.
public bool TryCopyTo(UnmanagedSpan<T> destination)
Parameters
destinationUnmanagedSpan<T>The span to copy items into.
Returns
- bool
If the destination span is shorter than the source span, this method return false and no data is written to the destination.
Operators
operator ==(UnmanagedSpan<T>, UnmanagedSpan<T>)
Returns true if left and right point at the same memory and have the same length. Note that this does not check to see if the contents are equal.
public static bool operator ==(UnmanagedSpan<T> left, UnmanagedSpan<T> right)
Parameters
leftUnmanagedSpan<T>rightUnmanagedSpan<T>
Returns
implicit operator ReadOnlyUnmanagedSpan<T>(UnmanagedSpan<T>)
Defines an implicit conversion of a UnmanagedSpan<T> to a ReadOnlyUnmanagedSpan<T>
public static implicit operator ReadOnlyUnmanagedSpan<T>(UnmanagedSpan<T> span)
Parameters
spanUnmanagedSpan<T>
Returns
implicit operator UnmanagedSpan<T>(ArraySegment<T>)
Defines an implicit conversion of a ArraySegment<T> to a UnmanagedSpan<T>
public static implicit operator UnmanagedSpan<T>(ArraySegment<T> segment)
Parameters
segmentArraySegment<T>
Returns
implicit operator UnmanagedSpan<T>(T[]?)
Defines an implicit conversion of an array to a UnmanagedSpan<T>
public static implicit operator UnmanagedSpan<T>(T[]? array)
Parameters
arrayT[]
Returns
operator !=(UnmanagedSpan<T>, UnmanagedSpan<T>)
Returns false if left and right point at the same memory and have the same length. Note that this does not check to see if the contents are equal.
public static bool operator !=(UnmanagedSpan<T> left, UnmanagedSpan<T> right)
Parameters
leftUnmanagedSpan<T>rightUnmanagedSpan<T>