Edytuj

Udostępnij za pośrednictwem


ReadOnlySpan<T>.Enumerator Struct

Definition

Provides an enumerator for the elements of a ReadOnlySpan<T>.

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

Type Parameters

T
Inheritance
ReadOnlySpan<T>.Enumerator

Remarks

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.)

Properties

Current

Gets a reference to the item at the current position of the enumerator.

Methods

MoveNext()

Advances the enumerator to the next item of the ReadOnlySpan<T>.

Applies to