Span<T>.Enumerator Structure
Définition
public: value class Span<T>::Enumerator
public struct Span<T>.Enumerator
type Span<'T>.Enumerator = struct
Public Structure Span(Of T).Enumerator
Paramètres de type
- T
- Héritage
Remarques
Le C# foreach du C# langage et l’instruction for each... La construction suivante dans Visual Basic masque la complexité des énumérateurs.The C# foreach of the C# language and the For Each...Next construct in Visual Basic hides the complexity of enumerators. Au lieu de manipuler directement l’énumérateur, l' foreach
utilisation For Each...Next
de ou de est recommandée.Instead of directly manipulating the enumerator, using foreach
or For Each...Next
is recommended.
Initialement, l’énumérateur est positionné avant le premier élément de Span<T>.Initially, the enumerator is positioned before the first element in the Span<T>. À cette position, Current n'est pas défini.At this position, Current is undefined. Vous devez appeler MoveNext pour avancer l’énumérateur jusqu’au premier élément de la Span<T> avant de lire la valeur Currentde.You must call MoveNext to advance the enumerator to the first item in the Span<T> before reading the value of Current.
Currentretourne la même valeur tant MoveNext que n’est pas appelé.Current returns the same value until MoveNext is called. MoveNextaffecte Current à l’élément suivant Span<T>dans.MoveNext sets Current to the next item in the Span<T>.
Si MoveNext passe la fin Span<T>de, MoveNext retourne false
.If MoveNext passes the end of the Span<T>, MoveNext returns false
. Lorsque l’énumérateur est à cet État, les appels suivants MoveNext à retournent false
également et Current n’est pas défini.When the enumerator is at this state, subsequent calls to MoveNext also return false
and Current is undefined. Vous ne pouvez Current pas définir à nouveau le premier Span<T> élément de. vous devez créer une nouvelle instance d’énumérateur à la place.You cannot set Current to the first item in the Span<T> again; you must create a new enumerator instance instead.
L’énumérateur ne dispose pas d’un Span<T>accès exclusif à.The enumerator does not have exclusive access to the Span<T>. En outre, les données sous-jacentes sur lesquelles repose l’étendue peuvent également être modifiées.In addition, the underlying data on which the span is based can also be modified. Par conséquent, l’énumération par le biais d’une étendue n’est pas intrinsèquement une procédure thread-safe.Therefore, enumerating through a span is intrinsically not a thread-safe procedure. Pour garantir la sécurité des threads pendant l’énumération, vous devez implémenter votre propre synchronisation.To guarantee thread safety during enumeration, you must implement your own synchronization. Par exemple, le code suivant a une condition de concurrence.For example, the following code has a race condition. Elle ne garantit pas que l’étendue sera énumérée avant l’exécution ClearContents
de la méthode.It does not ensure that the span will be enumerated before the ClearContents
method executes. Par conséquent, le tableau sous-jacent est effacé pendant l’énumération de l’étendue :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
Si vous synchronisez l’accès au tableau avant d’énumérer l’étendue, comme la version modifiée EnumerateSpan
de la méthode dans l’exemple suivant, ClearContents
la méthode ne modifie pas les données d’étendue sous-jacentes pendant l’énumération.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. Notez que l’exemple verrouille le tableau sous-jacent sur lequel l’étendue est basée.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
Contrairement à d’autres structures d’énumérateur dans .NET Span<T>.Enumerator, :Unlike some other enumerator structures in .NET, the Span<T>.Enumerator:
N’implémente pas IEnumerator l' IEnumerator<T> interface ou.Does not implement the IEnumerator or IEnumerator<T> interface. Cela est dû Span<T>.Enumerator au fait que est un struct Ref.This is because Span<T>.Enumerator is a ref struct.
N’inclut pas de
Reset
méthode, qui peut affecter à l’énumérateur sa position initiale avant le premier élément de l’étendue.Does not include aReset
method, which can set the enumerator to its initial position before the first element in the span. (La IEnumerator.Reset() méthode doit être implémentée dans le cadre de l’interface, mais la plupart des implémenteurs lèvent une exception ou ne fournissent aucune implémentation.)(The IEnumerator.Reset() method must be implemented as part of the interface, but most implementors either throw an exception or provide no implementation.)
Propriétés
Current |
Obtient une référence à l’élément à la position actuelle de l’énumérateur.Gets a reference to the item at the current position of the enumerator. |
Méthodes
MoveNext() |
Avance l’énumérateur à l’élément suivant de Span<T>.Advances the enumerator to the next item of the Span<T>. |