ReadOnlySpan<T>.Enumerator Struct

Definition

ReadOnlySpan<T> 的元素提供枚举器。Provides an enumerator for the elements of a ReadOnlySpan<T>.

public: value class ReadOnlySpan<T>::Enumerator
public struct ReadOnlySpan<T>.Enumerator
type ReadOnlySpan<'T>.Enumerator = struct
Public Structure ReadOnlySpan(Of T).Enumerator

Type Parameters

T
Inheritance
ReadOnlySpan<T>.Enumerator

Remarks

语言的foreach和 For Each ... C# C# For Each...NextVisual Basic 中的下一个构造隐藏了枚举器的复杂性。The C# foreach of the C# language and the For Each...Next construct in Visual Basic hides the complexity of enumerators. 建议使用 foreachFor Each...Next,而不是直接操作枚举器。Instead of directly manipulating the enumerator, using foreach or For Each...Next is recommended.

最初,枚举器定位在 ReadOnlySpan<T>中第一个元素之前。Initially, the enumerator is positioned before the first element in the ReadOnlySpan<T>. 在此位置上,未定义 CurrentAt this position, Current is undefined. 在读取 Current的值之前,必须调用 MoveNext 以将枚举数前移到 ReadOnlySpan<T> 中的第一项。You must call MoveNext to advance the enumerator to the first item in the ReadOnlySpan<T> before reading the value of Current.

Current 在调用 MoveNext 之前返回相同的值。Current returns the same value until MoveNext is called. MoveNextCurrent 设置到 ReadOnlySpan<T>中的下一项。MoveNext sets Current to the next item in the ReadOnlySpan<T>.

如果 MoveNext 越过 ReadOnlySpan<T>的末尾,MoveNext 将返回 falseIf MoveNext passes the end of the ReadOnlySpan<T>, MoveNext returns false. 当枚举器处于此状态时,对 MoveNext 的后续调用也将返回 false 并且 Current 未定义。When the enumerator is at this state, subsequent calls to MoveNext also return false and Current is undefined. 不能再次将 Current 设置到 ReadOnlySpan<T> 中的第一项;必须改为创建新的枚举器实例。You cannot set Current to the first item in the ReadOnlySpan<T> again; you must create a new enumerator instance instead.

尽管 ReadOnlySpan<T> 在堆栈上分配,但 ReadOnlySpan<T> 指向的基础数据可能不是。Though the ReadOnlySpan<T> is allocated on the stack, the underlying data on which the ReadOnlySpan<T> points to, may not be. 因此,枚举 ReadOnlySpan<T> 本质上不是一个线程安全的过程。Therefore, enumerating through a ReadOnlySpan<T> is intrinsically not a thread-safe procedure. 若要保证枚举过程中的线程安全,必须实现自己的同步。To guarantee thread safety during enumeration, you must implement your own synchronization.

不同于 .NET 中的其他枚举器结构,ReadOnlySpan<T>.EnumeratorUnlike some other enumerator structures in .NET, the ReadOnlySpan<T>.Enumerator:

Properties

Current

获取对枚举器当前位置的项目的引用。Gets a reference to the item at the current position of the enumerator.

Methods

MoveNext()

将枚举器推进到 ReadOnlySpan<T> 的下一项。Advances the enumerator to the next item of the ReadOnlySpan<T>.

Applies to