StringEnumerator.MoveNext 메서드

정의

열거자를 컬렉션의 다음 요소로 이동합니다.

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

반환

열거자가 다음 요소로 이동한 경우 true이(가) 반환되고, 컬렉션의 끝을 지난 경우 false이(가) 반환됩니다.

예외

열거자가 만들어진 후에 컬렉션이 수정되었습니다.

예제

다음 코드 예제에서는 의 몇 가지 속성 및 메서드를 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.

설명

열거자를 만들거나 가 호출된 후 Reset 열거자가 컬렉션의 첫 번째 요소 앞에 배치되고 첫 번째 호출을 통해 MoveNext 열거자가 컬렉션의 첫 번째 요소 위로 이동합니다.

경우 MoveNext 열거자를 컬렉션의 끝 컬렉션의 마지막 요소 뒤에 배치 되는 전달 하 고 MoveNext 반환 false합니다. 열거자가 있는 경우이 위치에 대 한 후속 호출은 MoveNext 반환할 수도 false 때까지 Reset 라고 합니다.

컬렉션이 변경되지 않고 그대로 유지되는 한 열거자는 유효한 상태로 유지됩니다. 변경에 추가 하는 등 컬렉션을 수정 하거나 요소를 삭제, 열거자가 복구할 유효 하지 않으며을 다음에 호출할 MoveNext 또는 Reset throw는 InvalidOperationException합니다. 컬렉션 사이 수정 된 경우 MoveNext 하 고 Current, Current 열거자가 이미 무효화 된 경우에로 설정 되어 있는 요소를 반환 합니다.

적용 대상

추가 정보