Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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 an enumerator for the elements of a ReadOnlySpan<T>.
public: value class ReadOnlySpan<T>::Enumerator : System::Collections::Generic::IEnumerator<T>
public: value class ReadOnlySpan<T>::Enumerator
public ref struct ReadOnlySpan<T>.Enumerator : System.Collections.Generic.IEnumerator<T>
public ref struct ReadOnlySpan<T>.Enumerator
type ReadOnlySpan<'T>.Enumerator = struct
interface IEnumerator<'T>
interface IEnumerator
interface IDisposable
type ReadOnlySpan<'T>.Enumerator = struct
Public Structure ReadOnlySpan(Of T).Enumerator
Implements IEnumerator(Of T)
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 foreach
or 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.
Current returns the same value until MoveNext is called. MoveNext sets Current to the next item in the ReadOnlySpan<T>.
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 implement the IEnumerator or IEnumerator<T> interface. This is because ReadOnlySpan<T>.Enumerator is a ref struct and cannot be boxed.
Does not include a Reset
method, 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.)
Current |
Gets a reference to the item at the current position of the enumerator. |
Move |
Advances the enumerator to the next item of the ReadOnlySpan<T>. |
Product | Versions |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 4.6.2 (package-provided), 4.7 (package-provided) |
.NET Standard | 2.0 (package-provided), 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in