ReadOnlySpan<T>.Enumerator 结构


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




语言的foreach和 For Each ... C# C# Visual 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. 在读取的MoveNext值之前,ReadOnlySpan<T>必须调用以将枚举器前进到中的第一项CurrentYou 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. MoveNext设置CurrentReadOnlySpan<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>.Enumerator其他枚举器结构,:Unlike some other enumerator structures in .NET, the ReadOnlySpan<T>.Enumerator:



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



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