StringEnumerator.MoveNext Método

Definición

Desplaza el enumerador al siguiente elemento de la colección.

public:
 bool MoveNext();
public bool MoveNext();
member this.MoveNext : unit -> bool
Public Function MoveNext () As Boolean

Devoluciones

true si el enumerador se ha avanzado correctamente al siguiente elemento; false si el enumerador ha pasado el final de la colección.

Excepciones

La colección se modificó después de crear el enumerador.

Ejemplos

En el ejemplo de código siguiente se muestran varias de las propiedades y métodos 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.

Comentarios

Después de crear o después de llamar a un Reset enumerador, se coloca un enumerador antes del primer elemento de la colección y la primera llamada para MoveNext mover el enumerador sobre el primer elemento de la colección.

Si MoveNext pasa el final de la colección, el enumerador se coloca después del último elemento de la colección y MoveNext devuelve false. Cuando el enumerador está en esta posición, las llamadas posteriores a también se devuelven MoveNextfalse hasta Reset que se llama a .

Un enumerador sigue siendo válido siempre que la colección permanezca sin cambios. Si se realizan cambios en la colección, como agregar, modificar o eliminar elementos, el enumerador se invalida irrecuperablemente y la siguiente llamada a MoveNext o Reset produce una InvalidOperationExceptionexcepción . Si la colección se modifica entre MoveNext y Current, Current devuelve el elemento en el que está establecido, incluso si el enumerador ya está invalidado.

Se aplica a

Consulte también