ReadOnlySpan<T> 结构

定义

提供任意内存的连续区域的类型安全和内存安全只读表示。Provides a type-safe and memory-safe read-only representation of a contiguous region of arbitrary memory.

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

类型参数

T

中项的类型ReadOnlySpan<T>The type of items in the ReadOnlySpan<T>.

继承
ReadOnlySpan<T>

注解

ReadOnlySpan<T>是在堆栈上分配的ref 结构,不能转义给托管堆。ReadOnlySpan<T> is a ref struct that is allocated on the stack and can never escape to the managed heap. Ref 结构类型具有多个限制,以确保无法将它们提升为托管堆,包括它们不能装箱、在 lambda 表达式中捕获、分配给类型Object的变量、 dynamic赋给变量和它们不能实现任何接口类型。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.

ReadOnlySpan<T>实例通常用于引用数组或数组部分的元素。A ReadOnlySpan<T> instance is often used to reference the elements of an array or a portion of an array. 但与数组不同, ReadOnlySpan<T>实例可以指向托管内存、本机内存或在堆栈上管理的内存。Unlike an array, however, a ReadOnlySpan<T> instance can point to managed memory, native memory, or memory managed on the stack.

构造函数

ReadOnlySpan<T>(T[])

在整个指定数组上创建新的 ReadOnlySpan<T>Creates a new ReadOnlySpan<T> over the entirety of a specified array.

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

从指定索引开始,创建包含数组的指定元素数的新 ReadOnlySpan<T>Creates a new ReadOnlySpan<T> that includes a specified number of elements of an array starting at a specified index.

ReadOnlySpan<T>(Void*, Int32)

从指定的内存地址开始,从指定的 T 元素数创建新的 ReadOnlySpan<T>Creates a new ReadOnlySpan<T> from a specified number of T elements starting at a specified memory address.

属性

Empty

返回一个空 ReadOnlySpan<T>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.

方法

CopyTo(Span<T>)

将此 ReadOnlySpan<T> 的内容复制到目标 Span<T>Copies the contents of this ReadOnlySpan<T> into a destination Span<T>.

Equals(Object)

不支持。Not supported. 引发 NotSupportedExceptionThrows a NotSupportedException.

GetEnumerator()

返回用于此 ReadOnlySpan<T> 的枚举器。Returns an enumerator for this ReadOnlySpan<T>.

GetHashCode()

不支持。Not supported. 引发 NotSupportedExceptionThrows a NotSupportedException.

GetPinnableReference()

针对类型 T 的对象的只读引用,该对象可用于进行固定,如果要在固定语句中使用范围,也必须使用它。A read-only reference to an object of type T that can be used for pinning, and is required to support the use of span within a fixed statement.

Slice(Int32)

从指定索引处开始的当前只读范围外形成切片。Forms a slice out of the current read-only span that begins at a specified index.

Slice(Int32, Int32)

从指定长度的指定索引处开始的当前只读范围外形成切片。Forms a slice out of the current read-only span starting at a specified index for a specified length.

ToArray()

将此只读范围的内容复制到新数组中。Copies the contents of this read-only span into a new array.

ToString()

返回此 ReadOnlySpan<T> 的字符串表示形式。Returns the string representation of this ReadOnlySpan<T>.

TryCopyTo(Span<T>)

尝试将此 ReadOnlySpan<T> 的内容复制到 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.

操作员

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

返回一个值,该值指示两个 ReadOnlySpan<T> 实例是否相等。Returns a value that indicates whether two ReadOnlySpan<T> instances are equal.

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

定义 ArraySegment<T>ReadOnlySpan<T> 的隐式转换。Defines an implicit conversion of an ArraySegment<T> to a ReadOnlySpan<T>.

Implicit(T[] to ReadOnlySpan<T>)

定义数组到 ReadOnlySpan<T> 的隐式转换。Defines an implicit conversion of an array to a ReadOnlySpan<T>.

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

返回一个值,该值指示两个 ReadOnlySpan<T> 实例是否不相等。Returns a value that indicates whether two ReadOnlySpan<T> instances are not equal.

扩展方法

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

使用指定的 IComparable<T> 泛型接口,在整个已排序的 ReadOnlySpan<T> 中搜索值。Searches an entire sorted ReadOnlySpan<T> for a value using the specified IComparable<T> generic interface.

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

使用指定的 TComparer 泛型类型,在整个已排序的 ReadOnlySpan<T> 中搜索指定值。Searches an entire sorted ReadOnlySpan<T> for a specified value using the specified TComparer generic type.

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

使用指定的 TComparable 泛型类型,在整个已排序的 ReadOnlySpan<T> 中搜索值。Searches an entire sorted ReadOnlySpan<T> for a value using the specified TComparable generic type.

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

指示是否在只读跨度内找到指定的值。Indicates whether a specified value is found in a read-only span. 使用 IEquatable{T}.Equals(T) 比较各值。Values are compared using IEquatable{T}.Equals(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)

搜索指定值并返回其第一次出现的索引。Searches for the specified value and returns the index of its first occurrence. 使用 IEquatable{T}.Equals(T) 比较各值。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. 使用 IEquatable{T}.Equals(T) 比较各值。Values are compared using IEquatable{T}.Equals(T).

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

搜索任何指定值的第一个索引,其方式类似于使用逻辑 OR 运算符多次调用 IndexOf。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, T)

搜索任何指定值的第一个索引,其方式类似于使用逻辑 OR 运算符多次调用 IndexOf。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>, ReadOnlySpan<T>)

搜索任何指定值的第一个索引,其方式类似于使用逻辑 OR 运算符多次调用 IndexOf。Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator.

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

搜索指定值并返回其最后一次出现的索引。Searches for the specified value and returns the index of its last occurrence. 使用 IEquatable{T}.Equals(T) 比较各值。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. 使用 IEquatable{T}.Equals(T) 比较各值。Values are compared using IEquatable{T}.Equals(T).

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

搜索任何指定值的最后一个索引,其方式类似于使用逻辑 OR 运算符多次调用 LastIndexOf。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, T)

搜索任何指定值的最后一个索引,其方式类似于使用逻辑 OR 运算符多次调用 LastIndexOf。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>, ReadOnlySpan<T>)

搜索任何指定值的最后一个索引,其方式类似于使用逻辑 OR 运算符多次调用 LastIndexOf。Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator.

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

确定两个只读序列是否在内存中重叠。Determines whether two read-only sequences overlap in memory.

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

确定两个只读序列是否在内存中重叠且输出元素偏移。Determines whether two read-only sequences overlap in memory and outputs the element offset.

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

通过使用 IComparable{T}.CompareTo(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>)

通过使用 IEquatable{T}.Equals(T) 比较元素,确定两个只读序列是否相等。Determines whether two read-only sequences are equal by comparing the elements using IEquatable{T}.Equals(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.

适用于