Only Span<T>.Enumerator Struct
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 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
The C# foreach of the C# language and the For Each...Next construct in Visual Basic hides the complexity of enumerators. Instead of directly manipulating the enumerator, using
For Each...Next is recommended.
Initially, the enumerator is positioned before the first element in the ReadOnlySpan<T>. At this position, Current is undefined. You must call MoveNext to advance the enumerator to the first item in the ReadOnlySpan<T> before reading the value of Current.
If MoveNext passes the end of the ReadOnlySpan<T>, MoveNext returns
false. When the enumerator is at this state, subsequent calls to MoveNext also return
false and Current is undefined. You cannot set Current to the first item in the ReadOnlySpan<T> again; you must create a new enumerator instance instead.
Though the ReadOnlySpan<T> is allocated on the stack, the underlying data on which the ReadOnlySpan<T> points to, may not be. 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.
Unlike some other enumerator structures in .NET, the ReadOnlySpan<T>.Enumerator:
Does not include a
Resetmethod, which can set the enumerator to its initial position before the first element in the span. (The IEnumerator.Reset() method must be implemented as part of the interface, but most implementors either throw an exception or provide no implementation.)
Gets a reference to the item at the current position of the enumerator.
Advances the enumerator to the next item of the ReadOnlySpan<T>.