Span<T> Struct

Definition

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

generic <typename T>
public value class Span
public readonly ref struct Span<T>
type Span<'T> = struct
Public Structure Span(Of T)

Type Parameters

T

The type of items in the Span<T>.

Inheritance
Span<T>

Remarks

For more information about this API, see Supplemental API remarks for Span<T>.

Constructors

Name Description
Span<T>(T[], Int32, Int32)

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

Span<T>(T[])

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

Span<T>(Void*, Int32)

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

Properties

Name Description
Empty

Returns an empty Span<T> object.

IsEmpty

Returns a value that indicates whether the current Span<T> is empty.

Item[Int32]

Gets the element at the specified zero-based index.

Length

Returns the length of the current span.

Methods

Name Description
Clear()

Clears the contents of this Span<T> object.

CopyTo(Span<T>)

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

Equals(Object)
Obsolete.

Calls to this method are not supported.

Fill(T)

Fills the elements of this span with a specified value.

GetEnumerator()

Returns an enumerator for this Span<T>.

GetHashCode()
Obsolete.

Throws a NotSupportedException.

GetPinnableReference()

Returns a 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 span starting at a specified index for a specified length.

Slice(Int32)

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

ToArray()

Copies the contents of this span into a new array.

ToString()

Returns the string representation of this Span<T> object.

TryCopyTo(Span<T>)

Attempts to copy the current Span<T> to a destination Span<T> and returns a value that indicates whether the copy operation succeeded.

Operators

Name Description
Equality(Span<T>, Span<T>)

Returns a value that indicates whether two Span<T> objects are equal.

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

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

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

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

Implicit(T[] to Span<T>)

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

Inequality(Span<T>, Span<T>)

Returns a value that indicates whether two Span<T> objects are not equal.

Extension Methods

Name Description
BinarySearch<T,TComparable>(Span<T>, TComparable)

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

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

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

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

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

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

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

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

Searches for the specified sequence and returns the index of its first occurrence.

IndexOf<T>(Span<T>, T)

Searches for the specified value and returns the index of its first occurrence.

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

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

IndexOfAny<T>(Span<T>, T, T, T)

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

IndexOfAny<T>(Span<T>, T, T)

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

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

Searches for the specified sequence and returns the index of its last occurrence.

LastIndexOf<T>(Span<T>, T)

Searches for the specified value and returns the index of its last occurrence.

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

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

LastIndexOfAny<T>(Span<T>, T, T, T)

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

LastIndexOfAny<T>(Span<T>, T, T)

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

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

Determines whether a span and a read-only span overlap in memory and outputs the element offset.

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

Determines whether a span and a read-only span overlap in memory.

Reverse<T>(Span<T>)

Reverses the sequence of the elements in the entire span.

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

Determines the relative order of a span and a read-only span by comparing the elements using IComparable{T}.CompareTo(T).

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

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

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

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

Applies to

See also