StringEnumerator.MoveNext Metoda

Definice

Posune enumerátor na další prvek kolekce.

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

Návraty

true pokud byl enumerátor úspěšně rozšířen na další prvek; false pokud enumerátor prošel koncem kolekce.

Výjimky

Kolekce byla změněna po vytvoření enumerátoru.

Příklady

Následující příklad kódu ukazuje několik vlastností a metod nástroje 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.

Poznámky

Po vytvoření enumerátoru nebo po Reset volání je enumerátor umístěn před prvním prvkem kolekce a první volání MoveNext přesune enumerátor nad první prvek kolekce.

Pokud MoveNext přejde na konec kolekce, enumerátor se umístí za poslední prvek v kolekci a MoveNext vrátí false. Když je enumerátor na této pozici, další volání, která se MoveNext také vrátí false , dokud Reset není volána.

Enumerátor zůstane platný, dokud kolekce zůstane beze změny. Pokud jsou v kolekci provedeny změny, například přidání, úprava nebo odstranění prvků, je výčet nenávratně zneplatněný a další volání MoveNext nebo Reset vyvolá InvalidOperationException. Pokud je kolekce změněna mezi MoveNext a Current, Current vrátí prvek, na který je nastavena, i když je výčet již neplatný.

Platí pro

Viz také