Span<T>.Enumerator Span<T>.Enumerator Span<T>.Enumerator Span<T>.Enumerator Struct

定义

Span<T> 的元素提供枚举器。Provides an enumerator for the elements of a Span<T>.

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

类型参数

T
继承
Span<T>.EnumeratorSpan<T>.EnumeratorSpan<T>.EnumeratorSpan<T>.Enumerator

注解

C# foreach 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.

最初,枚举数位于中的第一个元素之前Span<T>Initially, the enumerator is positioned before the first element in the Span<T>. 在此位置上,未定义 CurrentAt this position, Current is undefined. 必须调用MoveNext将枚举数前移到中的第一项Span<T>阅读的值之前,先CurrentYou must call MoveNext to advance the enumerator to the first item in the Span<T> before reading the value of Current.

Current 返回相同的值直到MoveNext调用。Current returns the same value until MoveNext is called. MoveNext 设置Current中的下一项Span<T>MoveNext sets Current to the next item in the Span<T>.

如果MoveNext传递的末尾Span<T>MoveNext返回falseIf MoveNext passes the end of the Span<T>, MoveNext returns false. 当该枚举数将在此状态下,后续调用MoveNext也会返回falseCurrent是不确定的。When the enumerator is at this state, subsequent calls to MoveNext also return false and Current is undefined. 不能设置Current中的第一个项目Span<T>再次; 必须改为创建新的枚举器实例。You cannot set Current to the first item in the Span<T> again; you must create a new enumerator instance instead.

枚举器不具有独占访问权Span<T>The enumerator does not have exclusive access to the Span<T>. 此外,还可以修改跨度所基于的基础数据。In addition, the underlying data on which the span is based can also be modified. 因此,通过一个范围枚举本质上不是线程安全的过程。Therefore, enumerating through a span is intrinsically not a thread-safe procedure. 若要确保枚举过程线程安全性,必须实现自己的同步。To guarantee thread safety during enumeration, you must implement your own synchronization. 例如,下面的代码具有一个争用条件。For example, the following code has a race condition. 不能确保跨度,将枚举之前ClearContents方法执行。It does not ensure that the span will be enumerated before the ClearContents 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<byte> 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<byte> 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

如果之前枚举的范围内,为修订版本的同步访问阵列EnumerateSpan方法的以下示例中,作用ClearContents方法不会修改在枚举过程基础 s p a n 的数据。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<byte> 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

与在.NET 中,某些其他枚举器结构不同Span<T>.Enumerator:Unlike some other enumerator structures in .NET, the Span<T>.Enumerator:

属性

Current Current Current Current

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

方法

MoveNext() MoveNext() MoveNext() MoveNext()

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

适用于