Edit

Share via


SpanExtensions Class

Definition

Helpers for working with the Span<T> type.

public static class SpanExtensions
type SpanExtensions = class
Public Module SpanExtensions
Inheritance
SpanExtensions

Methods

AsBytes<T>(Span<T>)

Casts a Span<T> of one primitive type T to Span<T> of bytes.

AsSpan2D<T>(Span<T>, Int32, Int32, Int32, Int32)

Returns a Span2D<T> instance wrapping the underlying data for the given Span<T> instance.

AsSpan2D<T>(Span<T>, Int32, Int32)

Returns a Span2D<T> instance wrapping the underlying data for the given Span<T> instance.

Cast<TFrom,TTo>(Span<TFrom>)

Casts a Span<T> of one primitive type TFrom to another primitive type TTo.

CopyTo<T>(Span<T>, RefEnumerable<T>)

Copies the contents of a given Span<T> into destination RefEnumerable<T> instance.

Count<T>(Span<T>, T)

Counts the number of occurrences of a given value into a target Span<T> instance.

DangerousGetReference<T>(Span<T>)

Returns a reference to the first element within a given Span<T>, with no bounds checks.

DangerousGetReferenceAt<T>(Span<T>, Int32)

Returns a reference to an element at a specified index within a given Span<T>, with no bounds checks.

DangerousGetReferenceAt<T>(Span<T>, IntPtr)

Returns a reference to an element at a specified index within a given Span<T>, with no bounds checks.

Enumerate<T>(Span<T>)

Enumerates the items in the input Span<T> instance, as pairs of reference/index values. This extension should be used directly within a foreach loop:

Span<int> numbers = new[] { 1, 2, 3, 4, 5, 6, 7 };

foreach (var item in numbers.Enumerate())
{
    // Access the index and value of each item here...
    int index = item.Index;
    ref int value = ref item.Value;
}

The compiler will take care of properly setting up the foreach loop with the type returned from this method.

GetDjb2HashCode<T>(Span<T>)

Gets a content hash from the input Span<T> instance using the Djb2 algorithm. For more info, see the documentation for GetDjb2HashCode<T>(ReadOnlySpan<T>).

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

Gets the index of an element of a given Span<T> from its reference.

Tokenize<T>(Span<T>, T)

Tokenizes the values in the input Span<T> instance using a specified separator. This extension should be used directly within a foreach loop:

Span<char> text = "Hello, world!".ToCharArray();

foreach (var token in text.Tokenize(','))
{
    // Access the tokens here...
}

The compiler will take care of properly setting up the foreach loop with the type returned from this method.

TryCopyTo<T>(Span<T>, RefEnumerable<T>)

Attempts to copy the contents of a given Span<T> into destination RefEnumerable<T> instance.

Applies to