StringEnumerator.Current Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient l'élément actuel dans la collection.
public:
property System::String ^ Current { System::String ^ get(); };
public string Current { get; }
member this.Current : string
Public ReadOnly Property Current As String
Valeur de propriété
Élément actuel de la collection.
Exceptions
L’énumérateur est positionné avant le premier élément de la collection ou après le dernier élément.
Exemples
L’exemple de code suivant illustre plusieurs propriétés et méthodes de StringEnumerator.
using System;
using System.Collections.Specialized;
public class SamplesStringEnumerator {
public static void Main() {
// Creates and initializes a StringCollection.
StringCollection myCol = new StringCollection();
String[] myArr = new String[] { "red", "orange", "yellow", "green", "blue", "indigo", "violet" };
myCol.AddRange( myArr );
// Enumerates the elements in the StringCollection.
StringEnumerator myEnumerator = myCol.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( "{0}", myEnumerator.Current );
Console.WriteLine();
// Resets the enumerator and displays the first element again.
myEnumerator.Reset();
if ( myEnumerator.MoveNext() )
Console.WriteLine( "The first element is {0}.", myEnumerator.Current );
}
}
/*
This code produces the following output.
red
orange
yellow
green
blue
indigo
violet
The first element is red.
*/
Imports System.Collections.Specialized
Public Class SamplesStringEnumerator
Public Shared Sub Main()
' Creates and initializes a StringCollection.
Dim myCol As New StringCollection()
Dim myArr() As [String] = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
myCol.AddRange(myArr)
' Enumerates the elements in the StringCollection.
Dim myEnumerator As StringEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine("{0}", myEnumerator.Current)
End While
Console.WriteLine()
' Resets the enumerator and displays the first element again.
myEnumerator.Reset()
If myEnumerator.MoveNext() Then
Console.WriteLine("The first element is {0}.", myEnumerator.Current)
End If
End Sub
End Class
'This code produces the following output.
'
'red
'orange
'yellow
'green
'blue
'indigo
'violet
'
'The first element is red.
Remarques
Une fois qu’un énumérateur est créé ou après un Reset appel, MoveNext il doit être appelé pour faire avancer l’énumérateur vers le premier élément de la collection avant de lire la valeur de Current; sinon, Current n’est pas défini.
Current lève également une exception si le dernier appel à MoveNext retourné false, qui indique la fin de la collection.
Currentne déplace pas la position de l’énumérateur et les appels consécutifs pour Current renvoyer le même objet tant qu’il n’est pas appelé ou MoveNext nonReset.
Un énumérateur reste valide tant que la collection reste inchangée. Si des modifications sont apportées à la collection, telles que l’ajout, la modification ou la suppression d’éléments, l’énumérateur est irrécupérablement invalidé et l’appel suivant à MoveNext ou Reset lève un InvalidOperationException. Si la collection est modifiée entre MoveNext et Current, Current retourne l’élément sur lequel elle est définie, même si l’énumérateur est déjà invalidé.