StringEnumerator.MoveNext Metodo

Definizione

Sposta l'enumeratore all'elemento successivo della raccolta.

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

Restituisce

true se l'enumeratore è stato spostato correttamente in avanti in corrispondenza dell'elemento successivo, false se l'enumeratore ha superato la fine della raccolta.

Eccezioni

La raccolta è stata modificata dopo la creazione dell'enumeratore.

Esempio

Nell'esempio di codice seguente vengono illustrate diverse proprietà e metodi di StringEnumerator.

#using <System.dll>

using namespace System;
using namespace System::Collections::Specialized;
int main()
{
   
   // Creates and initializes a StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   array<String^>^myArr = {"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.

*/
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.

Commenti

Dopo aver creato o dopo aver chiamato un Reset enumeratore, un enumeratore viene posizionato prima del primo elemento della raccolta e la prima chiamata per MoveNext spostare l'enumeratore sul primo elemento della raccolta.

Se MoveNext passa la fine della raccolta, l'enumeratore viene posizionato dopo l'ultimo elemento della raccolta e MoveNext restituisce false. Quando l'enumeratore si trova in questa posizione, le chiamate successive per restituire false anche fino a MoveNext quando Reset non viene chiamato.

Un enumeratore rimane valido finché la raccolta rimane invariata. Se le modifiche vengono apportate all'insieme, ad esempio l'aggiunta, la modifica o l'eliminazione di elementi, l'enumeratore viene invalidato in modo irreversibile e la chiamata successiva a MoveNext o Reset genera un InvalidOperationExceptionoggetto . Se la raccolta viene modificata tra MoveNext e Current, Current restituisce l'elemento impostato su, anche se l'enumeratore è già invalidato.

Si applica a

Vedi anche