OrderedDictionary.GetEnumerator Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengembalikan IDictionaryEnumerator objek yang melakukan iterasi OrderedDictionary melalui koleksi.
public:
virtual System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public virtual System.Collections.IDictionaryEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Public Overridable Function GetEnumerator () As IDictionaryEnumerator
Mengembalikan
Objek IDictionaryEnumerator untuk OrderedDictionary koleksi.
Penerapan
Contoh
Contoh kode berikut menunjukkan penggunaan GetEnumerator metode untuk menampilkan konten koleksi ke konsol OrderedDictionary . Dalam contoh ini, GetEnumerator metode digunakan untuk mendapatkan IDictionaryEnumerator objek yang diteruskan ke metode yang menampilkan konten. Kode ini adalah bagian dari contoh kode yang lebih besar yang dapat dilihat di OrderedDictionary.
// Clear the OrderedDictionary and add new values
myOrderedDictionary->Clear();
myOrderedDictionary->Add("newKey1", "newValue1");
myOrderedDictionary->Add("newKey2", "newValue2");
myOrderedDictionary->Add("newKey3", "newValue3");
// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator^ myEnumerator =
myOrderedDictionary->GetEnumerator();
Console::WriteLine(
"{0}Displaying the entries of a \"new\" OrderedDictionary.",
Environment::NewLine);
DisplayEnumerator(myEnumerator);
// Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear();
myOrderedDictionary.Add("newKey1", "newValue1");
myOrderedDictionary.Add("newKey2", "newValue2");
myOrderedDictionary.Add("newKey3", "newValue3");
// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator myEnumerator =
myOrderedDictionary.GetEnumerator();
Console.WriteLine(
"{0}Displaying the entries of a \"new\" OrderedDictionary.",
Environment.NewLine);
DisplayEnumerator(myEnumerator);
' Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear()
myOrderedDictionary.Add("newKey1", "newValue1")
myOrderedDictionary.Add("newKey2", "newValue2")
myOrderedDictionary.Add("newKey3", "newValue3")
' Display the contents of the "new" Dictionary Imports an enumerator
Dim myEnumerator As IDictionaryEnumerator = _
myOrderedDictionary.GetEnumerator()
Console.WriteLine( _
"{0}Displaying the entries of a 'new' OrderedDictionary.", _
Environment.NewLine)
DisplayEnumerator(myEnumerator)
// Displays the contents of the OrderedDictionary using its enumerator
static void DisplayEnumerator(IDictionaryEnumerator^ myEnumerator)
{
Console::WriteLine(" KEY VALUE");
while (myEnumerator->MoveNext())
{
Console::WriteLine(" {0,-25} {1}",
myEnumerator->Key, myEnumerator->Value);
}
}
// Displays the contents of the OrderedDictionary using its enumerator
public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator)
{
Console.WriteLine(" KEY VALUE");
while (myEnumerator.MoveNext())
{
Console.WriteLine(" {0,-25} {1}",
myEnumerator.Key, myEnumerator.Value);
}
}
' Displays the contents of the OrderedDictionary using its enumerator
Public Shared Sub DisplayEnumerator( _
ByVal myEnumerator As IDictionaryEnumerator)
Console.WriteLine(" KEY VALUE")
While myEnumerator.MoveNext()
Console.WriteLine(" {0,-25} {1}", _
myEnumerator.Key, myEnumerator.Value)
End While
End Sub
Keterangan
Pernyataan foreach
bahasa C# (for each
dalam Visual Basic) menyembunyikan kompleksitas enumerator. Oleh karena itu, penggunaan foreach
direkomendasikan alih-alih memanipulasi enumerator secara langsung.
Enumerator dapat digunakan untuk membaca data dalam koleksi, tetapi tidak dapat digunakan untuk memodifikasi koleksi yang mendasar.
Awalnya, enumerator diposisikan sebelum elemen pertama dalam koleksi.
Enumerator tetap valid selama koleksi tetap tidak berubah. Jika perubahan dilakukan pada koleksi, seperti menambahkan, memodifikasi, atau menghapus elemen, enumerator tidak valid dan perilakunya tidak terdefinisi.
Enumerator tidak memiliki akses eksklusif ke koleksi; oleh karena itu, menghitung melalui koleksi secara intrinsik bukan prosedur aman utas. Untuk menjamin keamanan utas selama enumerasi, Anda dapat mengunci koleksi selama seluruh enumerasi. Untuk memungkinkan koleksi diakses oleh beberapa utas untuk membaca dan menulis, Anda harus menerapkan sinkronisasi Anda sendiri.
Metode ini adalah operasi O(1).