Edit

Share via


ReadOnlySpan<T> Struct

Definition

Provides a type-safe and memory-safe read-only representation of a contiguous region of arbitrary memory.

public readonly ref struct ReadOnlySpan<T>
[System.Runtime.InteropServices.Marshalling.NativeMarshalling(typeof(System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>))]
public readonly ref struct ReadOnlySpan<T>

Type Parameters

T

The type of items in the ReadOnlySpan<T>.

Inheritance
ReadOnlySpan<T>
Attributes

Remarks

ReadOnlySpan<T> is a ref struct that is allocated on the stack and can never escape to the managed heap. Ref struct types have a number of restrictions to ensure that they cannot be promoted to the managed heap, including that they can't be boxed, captured in lambda expressions, assigned to variables of type Object, assigned to dynamic variables, and they cannot implement any interface type.

A ReadOnlySpan<T> instance is often used to reference the elements of an array or a portion of an array. Unlike an array, however, a ReadOnlySpan<T> instance can point to managed memory, native memory, or memory managed on the stack.

Constructors

ReadOnlySpan<T>(T)

Creates a new ReadOnlySpan<T> of length 1 around the specified reference.

ReadOnlySpan<T>(T[], Int32, Int32)

Creates a new ReadOnlySpan<T> that includes a specified number of elements of an array starting at a specified index.

ReadOnlySpan<T>(T[])

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

ReadOnlySpan<T>(Void*, Int32)

Creates a new ReadOnlySpan<T> from a specified number of T elements starting at a specified memory address.

Properties

Empty

Returns an empty ReadOnlySpan<T>.

IsEmpty

Returns a value that indicates the current read-only span is empty.

Item[Int32]

Gets an item from the read-only span at the specified zero-based index.

Length

The number of items in the read-only span.

Methods

CastUp<TDerived>(ReadOnlySpan<TDerived>)

Casts a read-only span of TDerived to a read-only span of T.

CopyTo(Span<T>)

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

Equals(Object)
Obsolete.
Obsolete.

Not supported. Throws a NotSupportedException.

GetEnumerator()

Returns an enumerator for this ReadOnlySpan<T>.

GetHashCode()
Obsolete.

Not supported. Throws a NotSupportedException.

GetPinnableReference()

Returns a read-only reference to an object of type T that can be used for pinning.

This method is intended to support .NET compilers and is not intended to be called by user code.

Slice(Int32, Int32)

Forms a slice out of the current read-only span starting at a specified index for a specified length.

Slice(Int32)

Forms a slice out of the current read-only span that begins at a specified index.

ToArray()

Copies the contents of this read-only span into a new array.

ToString()

Returns the string representation of this ReadOnlySpan<T>.

TryCopyTo(Span<T>)

Attempts to copy the contents of this ReadOnlySpan<T> into a Span<T> and returns a value to indicate whether or not the operation succeeded.

Operators

Equality(ReadOnlySpan<T>, ReadOnlySpan<T>)

Returns a value that indicates whether two ReadOnlySpan<T> instances are equal.

Implicit(ArraySegment<T> to ReadOnlySpan<T>)

Defines an implicit conversion of an ArraySegment<T> to a ReadOnlySpan<T>.

Implicit(T[] to ReadOnlySpan<T>)

Defines an implicit conversion of an array to a ReadOnlySpan<T>.

Inequality(ReadOnlySpan<T>, ReadOnlySpan<T>)

Returns a value that indicates whether two ReadOnlySpan<T> instances are not equal.

Extension Methods

ToImmutableArray<T>(ReadOnlySpan<T>)

Produce an immutable array of contents from specified elements.

BinarySearch<T>(ReadOnlySpan<T>, IComparable<T>)

Searches an entire sorted ReadOnlySpan<T> for a value using the specified IComparable<T> generic interface.

BinarySearch<T,TComparer>(ReadOnlySpan<T>, T, TComparer)

Searches an entire sorted ReadOnlySpan<T> for a specified value using the specified TComparer generic type.

BinarySearch<T,TComparable>(ReadOnlySpan<T>, TComparable)

Searches an entire sorted ReadOnlySpan<T> for a value using the specified TComparable generic type.

CommonPrefixLength<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IEqualityComparer<T>)

Determines the length of any common prefix shared between span and other.

CommonPrefixLength<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Finds the length of any common prefix shared between span and other.

Contains<T>(ReadOnlySpan<T>, T)

Indicates whether a specified value is found in a read-only span. Values are compared using IEquatable{T}.Equals(T).

ContainsAny<T>(ReadOnlySpan<T>, T, T, T)

Searches for an occurrence of value0, value1, or value2.

ContainsAny<T>(ReadOnlySpan<T>, T, T)

Searches for an occurrence of value0 or value1.

ContainsAny<T>(ReadOnlySpan<T>, SearchValues<T>)

Searches for an occurrence of any of the specified values.

ContainsAny<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for an occurrence of any of the specified values.

ContainsAnyExcept<T>(ReadOnlySpan<T>, T, T, T)

Searches for any value other than value0, value1, or value2.

ContainsAnyExcept<T>(ReadOnlySpan<T>, T, T)

Searches for any value other than value0 or value1.

ContainsAnyExcept<T>(ReadOnlySpan<T>, T)

Searches for any value other than the specified value.

ContainsAnyExcept<T>(ReadOnlySpan<T>, SearchValues<T>)

Searches for any value other than the specified values.

ContainsAnyExcept<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for any value other than the specified values.

ContainsAnyExceptInRange<T>(ReadOnlySpan<T>, T, T)

Searches for any value outside of the range between lowInclusive and highInclusive, inclusive.

ContainsAnyInRange<T>(ReadOnlySpan<T>, T, T)

Searches for any value in the range between lowInclusive and highInclusive, inclusive.

Count<T>(ReadOnlySpan<T>, T)

Counts the number of times the specified value occurs in the span.

Count<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Counts the number of times value occurs in span.

EndsWith<T>(ReadOnlySpan<T>, T)

Determines whether the specified value appears at the end of the span.

EndsWith<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines whether the specified sequence appears at the end of a read-only span.

IndexOf<T>(ReadOnlySpan<T>, T)

Searches for the specified value and returns the index of its first occurrence. Values are compared using IEquatable{T}.Equals(T).

IndexOf<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for the specified sequence and returns the index of its first occurrence. Values are compared using IEquatable{T}.Equals(T).

IndexOfAny<T>(ReadOnlySpan<T>, T, T, T)

Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator.

IndexOfAny<T>(ReadOnlySpan<T>, T, T)

Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator.

IndexOfAny<T>(ReadOnlySpan<T>, SearchValues<T>)

Searches for the first index of any of the specified values.

IndexOfAny<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator.

IndexOfAnyExcept<T>(ReadOnlySpan<T>, T, T, T)

Searches for the first index of any value other than the specified value0, value1, or value2.

IndexOfAnyExcept<T>(ReadOnlySpan<T>, T, T)

Searches for the first index of any value other than the specified value0 or value1.

IndexOfAnyExcept<T>(ReadOnlySpan<T>, T)

Searches for the first index of any value other than the specified value.

IndexOfAnyExcept<T>(ReadOnlySpan<T>, SearchValues<T>)

Searches for the first index of any value other than the specified values.

IndexOfAnyExcept<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for the first index of any value other than the specified values.

IndexOfAnyExceptInRange<T>(ReadOnlySpan<T>, T, T)

Searches for the first index of any value outside of the range between lowInclusive and highInclusive, inclusive.

IndexOfAnyInRange<T>(ReadOnlySpan<T>, T, T)

Searches for the first index of any value in the range between lowInclusive and highInclusive, inclusive.

LastIndexOf<T>(ReadOnlySpan<T>, T)

Searches for the specified value and returns the index of its last occurrence. Values are compared using IEquatable{T}.Equals(T).

LastIndexOf<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for the specified sequence and returns the index of its last occurrence. Values are compared using IEquatable{T}.Equals(T).

LastIndexOfAny<T>(ReadOnlySpan<T>, T, T, T)

Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator.

LastIndexOfAny<T>(ReadOnlySpan<T>, T, T)

Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator.

LastIndexOfAny<T>(ReadOnlySpan<T>, SearchValues<T>)

Searches for the last index of any of the specified values.

LastIndexOfAny<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator.

LastIndexOfAnyExcept<T>(ReadOnlySpan<T>, T, T, T)

Searches for the last index of any value other than the specified value0, value1, or value2.

LastIndexOfAnyExcept<T>(ReadOnlySpan<T>, T, T)

Searches for the last index of any value other than the specified value0 or value1.

LastIndexOfAnyExcept<T>(ReadOnlySpan<T>, T)

Searches for the last index of any value other than the specified value.

LastIndexOfAnyExcept<T>(ReadOnlySpan<T>, SearchValues<T>)

Searches for the last index of any value other than the specified values.

LastIndexOfAnyExcept<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Searches for the last index of any value other than the specified values.

LastIndexOfAnyExceptInRange<T>(ReadOnlySpan<T>, T, T)

Searches for the last index of any value outside of the range between lowInclusive and highInclusive, inclusive.

LastIndexOfAnyInRange<T>(ReadOnlySpan<T>, T, T)

Searches for the last index of any value in the range between lowInclusive and highInclusive, inclusive.

Overlaps<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, Int32)

Determines whether two read-only sequences overlap in memory and outputs the element offset.

Overlaps<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines whether two read-only sequences overlap in memory.

Replace<T>(ReadOnlySpan<T>, Span<T>, T, T)

Copies source to destination, replacing all occurrences of oldValue with newValue.

SequenceCompareTo<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines the relative order of two read-only sequences by comparing their elements using IComparable{T}.CompareTo(T).

SequenceEqual<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IEqualityComparer<T>)

Determines whether two sequences are equal by comparing the elements using an IEqualityComparer<T>.

SequenceEqual<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines whether two read-only sequences are equal by comparing the elements using IEquatable{T}.Equals(T).

Split<T>(ReadOnlySpan<T>, T)

Returns a type that allows for enumeration of each element within a split span using the provided separator character.

Split<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Returns a type that allows for enumeration of each element within a split span using the provided separator span.

SplitAny<T>(ReadOnlySpan<T>, SearchValues<T>)

Returns a type that allows for enumeration of each element within a split span using the provided separator characters.

SplitAny<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Returns a type that allows for enumeration of each element within a split span using any of the provided elements.

StartsWith<T>(ReadOnlySpan<T>, T)

Determines whether the specified value appears at the start of the span.

StartsWith<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines whether a specified sequence appears at the start of a read-only span.

Trim<T>(ReadOnlySpan<T>, T)

Removes all leading and trailing occurrences of a specified element from a read-only span.

Trim<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Removes all leading and trailing occurrences of a set of elements specified in a read-only span from a read-only span.

TrimEnd<T>(ReadOnlySpan<T>, T)

Removes all trailing occurrences of a specified element from a read-only span.

TrimEnd<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Removes all trailing occurrences of a set of elements specified in a read-only span from a read-only span.

TrimStart<T>(ReadOnlySpan<T>, T)

Removes all leading occurrences of a specified element from the span.

TrimStart<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Removes all leading occurrences of a set of elements specified in a read-only span from the span.

Applies to

Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.7 (package-provided), 4.7.1 (package-provided), 4.7.2 (package-provided), 4.8 (package-provided)
.NET Standard 2.0 (package-provided), 2.1

See also