Span.Enumerator Struct
Definition
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 Span
public: value class Span::Enumerator : System::Collections::Generic::IEnumerator
public: value class Span::Enumerator
public ref struct Span.Enumerator : System.Collections.Generic.IEnumerator
public ref struct Span.Enumerator
type Span<'T>.Enumerator = struct
interface IEnumerator<'T>
interface IEnumerator
interface IDisposable
type Span<'T>.Enumerator = struct
Public Structure Span(Of T).Enumerator
Implements IEnumerator(Of T)
Public Structure Span(Of T).Enumerator
Type Parameters
- T
- Inheritance
- Implements
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 Span
Current returns the same value until MoveNext is called. MoveNext sets Current to the next item in the Span
If MoveNext passes the end of the Spanfalse. 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 Span
The enumerator does not have exclusive access to the SpanClearContents method executes. As a result, the underlying array is cleared during enumeration of the span:
using System;
using System.Threading.Tasks;
class Program
{
private static readonly byte[] _array = new byte[5];
static void Main()
{
new Random(42).NextBytes(_array);
Span span = _array;
Task.Run( () => ClearContents() );
EnumerateSpan(span);
}
public static void ClearContents()
{
Task.Delay(20).Wait();
lock (_array)
{
Array.Clear(_array, 0, _array.Length);
}
}
public static void EnumerateSpan(Span span)
{
foreach (byte element in span)
{
Console.WriteLine(element);
Task.Delay(10).Wait();
}
}
}
// The example displays output like the following:
// 62
// 23
// 186
// 0
// 0
module Program
open System
open System.Threading.Tasks
let array = Array.zeroCreate 5
let clearContents () =
Task.Delay(20).Wait()
lock array (fun () ->
Array.Clear(array, 0, array.Length) )
let enumerateSpan (span: Span) =
for element in span do
printfn $"{element}"
Task.Delay(10).Wait()
[]
let main _ =
Random(42).NextBytes array
printfn "%A" array
let span: Span = array
Task.Run clearContents |> ignore
enumerateSpan span
0
// The example displays output like the following:
// 62
// 23
// 186
// 0
// 0
If you synchronize access to the array before enumerating the span, as the revised version of the EnumerateSpan method does in the following example, the ClearContents method doesn't modify underlying span data during enumeration. Note that the example locks the underlying array on which the span is based.
public static void EnumerateSpan(Span span)
{
lock (_array)
{
foreach (byte element in span)
{
Console.WriteLine(element);
Task.Delay(10).Wait();
}
}
}
// The example displays the following output:
// 62
// 23
// 186
// 150
// 174
let enumerateSpan (span: Span) =
// Spans cannot be accessed in closures including in the F# lock function.
// Monitor.Enter and Monitor.Exit are used here directly.
Monitor.Enter array
try
for element in span do
printfn $"{element}"
Task.Delay(10).Wait()
finally
Monitor.Exit array
// The example displays the following output:
// 62
// 23
// 186
// 150
// 174
Unlike some other enumerator structures in .NET, the Span
Does not implement the IEnumerator or IEnumerator
interface. This is because Span .Enumerator is a ref struct.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.)
Properties
| Name | Description |
|---|---|
| Current |
Gets a reference to the item at the current position of the enumerator. |
Methods
| Name | Description |
|---|---|
| MoveNext() |
Advances the enumerator to the next item of the Span |
Explicit Interface Implementations
| Name | Description |
|---|---|
| IDisposable.Dispose() |
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| IEnumerator.Current |
Gets the element in the collection at the current position of the enumerator. |
| IEnumerator.Reset() |
Sets the enumerator to its initial position, which is before the first element in the collection. |
|
IEnumerator |
Gets the element in the collection at the current position of the enumerator. |