Table of Contents

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

T

The 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

ptr void*

Pointer to the first element.

length int

Number of elements.

ReadOnlySpan(T[])

Creates a new ReadOnlySpan<T> over the entirety of a target array.

public ReadOnlySpan(T[] array)

Parameters

array T[]

The target array.

Fields

Length

Gets the number of elements in the span.

public readonly int Length

Field Value

int

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

bool

this[int]

Returns the element at the specified index.

public T this[int index] { get; }

Parameters

index int

Zero-based index of the element.

Property Value

T

Exceptions

IndexOutOfRangeException

Thrown when index is 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

items ReadOnlySpan<TDerived>

The source span to cast from.

Returns

ReadOnlySpan<T>

Never returns — always throws NotSupportedException.

Type Parameters

TDerived

The derived type to cast from. Must satisfy where TDerived : class, T, which is incompatible with the unmanaged constraint of this shim.

CopyTo(Span<T>)

Copies the contents of this span into a destination Span<T>.

public void CopyTo(Span<T> destination)

Parameters

destination Span<T>

The target span.

Exceptions

ArgumentException

Thrown when destination is 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

destination T[]

The target array.

destinationIndex int

Index in destination at which copying begins.

Exceptions

ArgumentNullException

Thrown when destination is null.

ArgumentException

Thrown when the destination array does not have enough capacity from destinationIndex to 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

other ReadOnlySpan<T>

The span to compare against.

Returns

bool

true if both spans have the same length and identical contents; otherwise false.

Slice(int)

Forms a slice out of the current span starting at the specified index.

public ReadOnlySpan<T> Slice(int start)

Parameters

start int

The index at which to begin the slice.

Returns

ReadOnlySpan<T>

A ReadOnlySpan<T> that consists of all elements from start to the end of the span.

Exceptions

ArgumentOutOfRangeException

Thrown when start is 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

start int

The index at which to begin the slice.

length int

The desired length of the slice.

Returns

ReadOnlySpan<T>

A ReadOnlySpan<T> of length elements starting at start.

Exceptions

ArgumentOutOfRangeException

Thrown when start or length is out of range.

ToArray()

Copies the contents of this span into a new array.

public T[] ToArray()

Returns

T[]

A new T array 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

destination Span<T>

The target span.

Returns

bool

true if the copy succeeded; false if destination is shorter than this span.

Operators

implicit operator ReadOnlySpan<T>(T[])

Implicitly converts a T array to a ReadOnlySpan<T>.

public static implicit operator ReadOnlySpan<T>(T[] array)

Parameters

array T[]

The source array.

Returns

ReadOnlySpan<T>

A ReadOnlySpan<T> over the entire array.