StringEnumerator.MoveNext メソッド

定義

列挙子をコレクションの次の要素に進めます。

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

返品

true 列挙子が次の要素に正常に進んだ場合。列挙子がコレクションの末尾を通過した場合に false します。

例外

列挙子の作成後にコレクションが変更されました。

次のコード例では、 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.

注釈

列挙子が作成された後、または Reset が呼び出された後、列挙子はコレクションの最初の要素の前に配置され、 MoveNext の最初の呼び出しは、列挙子をコレクションの最初の要素の上に移動します。

MoveNextコレクションの末尾を渡すと、列挙子はコレクション内の最後の要素の後に配置され、MoveNextfalseを返します。 列挙子がこの位置にある場合、後続のMoveNextの呼び出しでは、falseが呼び出されるまでResetも返されます。

列挙子は、コレクションが変更されない限り有効なままです。 要素の追加、変更、削除など、コレクションに変更が加えられた場合、列挙子は回復不能に無効になり、次に MoveNext または Reset 呼び出すと InvalidOperationExceptionがスローされます。 コレクションが MoveNextCurrentの間で変更された場合、列挙子が既に無効になっている場合でも、 Current は設定されている要素を返します。

適用対象

こちらもご覧ください