Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

ReadOnlySpan<T> Struct

Definition

Namespace:
System
Assemblies:
netstandard.dll, System.Runtime.dll
Assembly:
System.Runtime.dll
Assembly:
System.Memory.dll
Assembly:
netstandard.dll
Package:
System.Memory v4.6.3
Source:
ReadOnlySpan.cs
Source:
ReadOnlySpan.cs
Source:
ReadOnlySpan.cs
Source:
ReadOnlySpan.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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

generic <typename T>public value class ReadOnlySpan
[System.Runtime.InteropServices.Marshalling.NativeMarshalling(typeof(System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>))]public readonly ref struct ReadOnlySpan<T>
public readonly ref struct ReadOnlySpan<T>
[<System.Runtime.InteropServices.Marshalling.NativeMarshalling(typeof(System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>))>]type ReadOnlySpan<'T> = struct
type ReadOnlySpan<'T> = struct
Public Structure ReadOnlySpan(Of T)

Type Parameters

T

The type of items in theReadOnlySpan<T>.

Inheritance
ReadOnlySpan<T>
Attributes

Remarks

ReadOnlySpan<T> is aref 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 typeObject, assigned todynamic variables, and they cannot implement any interface type.

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

Constructors

ReadOnlySpan<T>(T)

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

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

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

ReadOnlySpan<T>(T[])

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

ReadOnlySpan<T>(Void*, Int32)

Creates a newReadOnlySpan<T> from a specified number ofT elements starting at a specified memory address.

Properties

Empty

Returns an emptyReadOnlySpan<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 ofTDerived to a read-only span ofT.

CopyTo(Span<T>)

Copies the contents of thisReadOnlySpan<T> into a destinationSpan<T>.

Equals(Object)
Obsolete.
Obsolete.

Not supported. Throws aNotSupportedException.

GetEnumerator()

Returns an enumerator for thisReadOnlySpan<T>.

GetHashCode()
Obsolete.

Not supported. Throws aNotSupportedException.

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 thisReadOnlySpan<T>.

TryCopyTo(Span<T>)

Attempts to copy the contents of thisReadOnlySpan<T> into aSpan<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 twoReadOnlySpan<T> instances are equal.

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

Defines an implicit conversion of anArraySegment<T> to aReadOnlySpan<T>.

Implicit(T[] to ReadOnlySpan<T>)

Defines an implicit conversion of an array to aReadOnlySpan<T>.

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

Returns a value that indicates whether twoReadOnlySpan<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 sortedReadOnlySpan<T> for a value using the specifiedIComparable<T> generic interface.

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

Searches an entire sortedReadOnlySpan<T> for a specified value using the specifiedTComparer generic type.

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

Searches an entire sortedReadOnlySpan<T> for a value using the specifiedTComparable generic type.

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

Determines the length of any common prefix shared betweenspan andother.

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

Finds the length of any common prefix shared betweenspan andother.

Contains<T>(ReadOnlySpan<T>, T, IEqualityComparer<T>)
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, IEqualityComparer<T>)
ContainsAny<T>(ReadOnlySpan<T>, T, T, T)

Searches for an occurrence ofvalue0,value1, orvalue2.

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

Searches for an occurrence ofvalue0 orvalue1.

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

Searches for an occurrence of any of the specifiedvalues.

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

Searches for an occurrence of any of the specifiedvalues.

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

Searches for any value other thanvalue0,value1, orvalue2.

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

Searches for any value other thanvalue0 orvalue1.

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

Searches for any value other than the specifiedvalue.

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

Searches for any value other than the specifiedvalues.

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

Searches for any value other than the specifiedvalues.

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

Searches for any value outside of the range betweenlowInclusive andhighInclusive, inclusive.

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

Searches for any value in the range betweenlowInclusive andhighInclusive, inclusive.

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

Counts the number of times the specifiedvalue occurs in thespan.

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

Counts the number of timesvalue occurs inspan.

CountAny<T>(ReadOnlySpan<T>, SearchValues<T>)
CountAny<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IEqualityComparer<T>)
CountAny<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)
EndsWith<T>(ReadOnlySpan<T>, T, IEqualityComparer<T>)
EndsWith<T>(ReadOnlySpan<T>, T)

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

EndsWith<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IEqualityComparer<T>)
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, IEqualityComparer<T>)
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>, IEqualityComparer<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, IEqualityComparer<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, IEqualityComparer<T>)
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>, IEqualityComparer<T>)
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, IEqualityComparer<T>)
IndexOfAnyExcept<T>(ReadOnlySpan<T>, T, T, T)

Searches for the first index of any value other than the specifiedvalue0,value1, orvalue2.

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

Searches for the first index of any value other than the specifiedvalue0 orvalue1.

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

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

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

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

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

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

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

Searches for the first index of any value outside of the range betweenlowInclusive andhighInclusive, inclusive.

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

Searches for the first index of any value in the range betweenlowInclusive andhighInclusive, inclusive.

LastIndexOf<T>(ReadOnlySpan<T>, T, IEqualityComparer<T>)
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>, IEqualityComparer<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, IEqualityComparer<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, IEqualityComparer<T>)
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>, IEqualityComparer<T>)
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, IEqualityComparer<T>)
LastIndexOfAnyExcept<T>(ReadOnlySpan<T>, T, T, T)

Searches for the last index of any value other than the specifiedvalue0,value1, orvalue2.

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

Searches for the last index of any value other than the specifiedvalue0 orvalue1.

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

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

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

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

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

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

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

Searches for the last index of any value outside of the range betweenlowInclusive andhighInclusive, inclusive.

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

Searches for the last index of any value in the range betweenlowInclusive andhighInclusive, 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, IEqualityComparer<T>)
Replace<T>(ReadOnlySpan<T>, Span<T>, T, T)

Copiessource todestination, replacing all occurrences ofoldValue withnewValue.

ReplaceAny<T>(ReadOnlySpan<T>, Span<T>, SearchValues<T>, T)
ReplaceAnyExcept<T>(ReadOnlySpan<T>, Span<T>, SearchValues<T>, T)
SequenceCompareTo<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IComparer<T>)
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 anIEqualityComparer<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, IEqualityComparer<T>)
StartsWith<T>(ReadOnlySpan<T>, T)

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

StartsWith<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, IEqualityComparer<T>)
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

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo