Struct ReadOnlySpan<T>
- Namespace
- System
- Assembly
- C2JBridge.dll
Compatibility shim of ReadOnlySpan<T> for .NET Framework targets.
This type is provided solely to allow JCOBridge public APIs that expose ReadOnlySpan<T> (such as AsSpan() and AsSpan()) to compile and function correctly on .NET Framework without any external dependency. The implementation is backed by native pointer arithmetic and covers the subset of the API required by JCOBridge consumers.
JCOBridge HPA edition on .NET Framework uses the official
System.ReadOnlySpan<T> from the System.Memory NuGet package,
which provides the full runtime-optimised implementation including SIMD support.
Users targeting .NET 5+ or .NET Standard 2.1 always receive the official runtime type — this shim is never compiled into those targets.
public readonly ref struct ReadOnlySpan<T> where T : unmanaged
Type Parameters
TThe type of elements in the span. Must be an unmanaged type.
- Inherited Members
Constructors
ReadOnlySpan(void*, int)
Creates a new ReadOnlySpan<T> from a raw pointer and a length.
public ReadOnlySpan(void* ptr, int length)
Parameters
ReadOnlySpan(T[])
Creates a new ReadOnlySpan<T> over the entirety of a target array.
public ReadOnlySpan(T[] array)
Parameters
arrayT[]The target array.
Fields
Length
Gets the number of elements in the span.
public readonly int Length
Field Value
Properties
Empty
Gets an empty ReadOnlySpan<T>.
public static ReadOnlySpan<T> Empty { get; }
Property Value
- ReadOnlySpan<T>
IsEmpty
Gets a value indicating whether this span is empty.
public bool IsEmpty { get; }
Property Value
this[int]
Returns the element at the specified index.
public T this[int index] { get; }
Parameters
indexintZero-based index of the element.
Property Value
- T
Exceptions
- IndexOutOfRangeException
Thrown when
indexis negative or greater than or equal to Length.
Methods
CastUp<TDerived>(ReadOnlySpan<TDerived>)
Not supported on .NET Framework.
CastUp<TDerived> exists for reference-type covariance
(e.g. ReadOnlySpan<string> → ReadOnlySpan<object>),
which is incompatible with the where T : unmanaged constraint
required by this shim. Unmanaged types are never reference types and
therefore covariant casting is meaningless in this context.
[Obsolete("CastUp is not supported on this shim: unmanaged types cannot satisfy 'where TDerived : class, T'.", true)]
public static ReadOnlySpan<T> CastUp<TDerived>(ReadOnlySpan<TDerived> items) where TDerived : unmanaged
Parameters
itemsReadOnlySpan<TDerived>The source span to cast from.
Returns
- ReadOnlySpan<T>
Never returns — always throws NotSupportedException.
Type Parameters
TDerivedThe derived type to cast from. Must satisfy
where TDerived : class, T, which is incompatible with theunmanagedconstraint of this shim.
CopyTo(Span<T>)
Copies the contents of this span into a destination Span<T>.
public void CopyTo(Span<T> destination)
Parameters
destinationSpan<T>The target span.
Exceptions
- ArgumentException
Thrown when
destinationis shorter than this span.
CopyTo(T[], int)
Copies the contents of this span into a destination array.
public void CopyTo(T[] destination, int destinationIndex = 0)
Parameters
destinationT[]The target array.
destinationIndexintIndex in
destinationat which copying begins.
Exceptions
- ArgumentNullException
Thrown when
destinationis null.- ArgumentException
Thrown when the destination array does not have enough capacity from
destinationIndexto hold all elements.
GetEnumerator()
Returns an enumerator for this span, enabling foreach support.
public ReadOnlySpan<T>.Enumerator GetEnumerator()
Returns
- ReadOnlySpan<T>.Enumerator
An ReadOnlySpan<T>.Enumerator for this ReadOnlySpan<T>.
GetPinnableReference()
Returns a reference to the first element for use with the fixed statement. Not intended to be called by user code.
public ref readonly T GetPinnableReference()
Returns
- T
A reference to the first element, or a null reference if the span is empty.
SequenceEqual(ReadOnlySpan<T>)
Determines whether this span and other contain the same sequence of elements,
using a native memcmp comparison.
public bool SequenceEqual(ReadOnlySpan<T> other)
Parameters
otherReadOnlySpan<T>The span to compare against.
Returns
Slice(int)
Forms a slice out of the current span starting at the specified index.
public ReadOnlySpan<T> Slice(int start)
Parameters
startintThe index at which to begin the slice.
Returns
- ReadOnlySpan<T>
A ReadOnlySpan<T> that consists of all elements from
startto the end of the span.
Exceptions
- ArgumentOutOfRangeException
Thrown when
startis negative or greater than Length.
Slice(int, int)
Forms a slice out of the current span starting at the specified index for the specified length.
public ReadOnlySpan<T> Slice(int start, int length)
Parameters
Returns
- ReadOnlySpan<T>
A ReadOnlySpan<T> of
lengthelements starting atstart.
Exceptions
- ArgumentOutOfRangeException
Thrown when
startorlengthis out of range.
ToArray()
Copies the contents of this span into a new array.
public T[] ToArray()
Returns
- T[]
A new
Tarray containing the elements of this span.
TryCopyTo(Span<T>)
Attempts to copy the contents of this span into a destination Span<T> and returns a value indicating whether the operation succeeded.
public bool TryCopyTo(Span<T> destination)
Parameters
destinationSpan<T>The target span.
Returns
Operators
implicit operator ReadOnlySpan<T>(T[])
Implicitly converts a T array to a ReadOnlySpan<T>.
public static implicit operator ReadOnlySpan<T>(T[] array)
Parameters
arrayT[]The source array.
Returns
- ReadOnlySpan<T>
A ReadOnlySpan<T> over the entire
array.