Bagikan melalui


StringEnumerator.MoveNext Metode

Definisi

Memajukan enumerator ke elemen koleksi berikutnya.

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

Mengembalikan

true jika enumerator berhasil dimajukan ke elemen berikutnya; false jika enumerator telah melewati akhir koleksi.

Pengecualian

Koleksi dimodifikasi setelah enumerator dibuat.

Contoh

Contoh kode berikut menunjukkan beberapa properti dan metode 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.

Keterangan

Setelah enumerator dibuat atau setelah Reset dipanggil, enumerator diposisikan sebelum elemen pertama koleksi, dan panggilan pertama untuk MoveNext memindahkan enumerator ke elemen pertama koleksi.

Jika MoveNext melewati akhir koleksi, enumerator diposisikan setelah elemen terakhir dalam koleksi dan MoveNext mengembalikan false. Ketika enumerator berada di posisi ini, panggilan berikutnya untuk MoveNext juga kembali false sampai Reset dipanggil.

Enumerator tetap valid selama koleksi tetap tidak berubah. Jika perubahan dilakukan pada koleksi, seperti menambahkan, memodifikasi, atau menghapus elemen, enumerator tidak valid dan panggilan berikutnya ke MoveNext atau Reset melemparkan InvalidOperationException. Jika koleksi dimodifikasi antara MoveNext dan Current, Current mengembalikan elemen yang diatur ke, bahkan jika enumerator sudah tidak valid.

Berlaku untuk

Lihat juga