Dictionary<TKey,TValue>.IDictionary.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 untuk IDictionary.
virtual System::Collections::IDictionaryEnumerator ^ System.Collections.IDictionary.GetEnumerator() = System::Collections::IDictionary::GetEnumerator;
System.Collections.IDictionaryEnumerator IDictionary.GetEnumerator();
abstract member System.Collections.IDictionary.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
override this.System.Collections.IDictionary.GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Function GetEnumerator () As IDictionaryEnumerator Implements IDictionary.GetEnumerator
Mengembalikan
Untuk IDictionaryEnumeratorIDictionary.
Penerapan
Contoh
Contoh kode berikut menunjukkan cara menghitung pasangan kunci/nilai dalam kamus dengan menggunakan foreach pernyataan (For Each di Visual Basic), yang menyembunyikan penggunaan enumerator. Secara khusus, perhatikan bahwa enumerator untuk System.Collections.IDictionary antarmuka mengembalikan DictionaryEntry objek daripada KeyValuePair<TKey,TValue> objek.
using System;
using System.Collections;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new dictionary of strings, with string keys,
// and access it using the IDictionary interface.
//
IDictionary openWith = new Dictionary<string, string>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
// IDictionary.Add throws an exception if incorrect types
// are supplied for key or value.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// When you use foreach to enumerate dictionary elements
// with the IDictionary interface, the elements are retrieved
// as DictionaryEntry objects instead of KeyValuePair objects.
foreach( DictionaryEntry de in openWith )
{
Console.WriteLine($"Key = {de.Key}, Value = {de.Value}");
}
}
}
open System
open System.Collections
open System.Collections.Generic
// Create a new dictionary of strings, with string keys,
// and access it using the IDictionary interface.
let openWith: IDictionary = Dictionary<string, string>()
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
// IDictionary.Add throws an exception if incorrect types
// are supplied for key or value.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
// When you use foreach to enumerate dictionary elements
// with the IDictionary interface, the elements are retrieved
// as DictionaryEntry objects instead of KeyValuePair objects.
printfn ""
for de in openWith do
let de = de :?> DictionaryEntry
printfn $"For key = {de.Key}, value = {de.Value}"
Imports System.Collections
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new dictionary of strings, with string keys,
' and access it using the IDictionary interface.
'
Dim openWith As IDictionary = _
New Dictionary(Of String, String)
' Add some elements to the dictionary. There are no
' duplicate keys, but some of the values are duplicates.
' IDictionary.Add throws an exception if incorrect types
' are supplied for key or value.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' When you use foreach to enumerate dictionary elements
' with the IDictionary interface, the elements are retrieved
' as DictionaryEntry objects instead of KeyValuePair objects.
Console.WriteLine()
For Each de As DictionaryEntry In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
de.Key, de.Value)
Next
End Sub
End Class
Keterangan
Untuk tujuan enumerasi, setiap item adalah DictionaryEntry struktur.
Pernyataan foreach bahasa C# (For Each dalam Visual Basic) menyembunyikan kompleksitas enumerator. Oleh karena itu, penggunaan foreach disarankan, alih-alih langsung memanipulasi enumerator.
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. Metode ini Reset juga membawa enumerator kembali ke posisi ini. Pada posisi ini, Entry tidak terdefinisi. Oleh karena itu, Anda harus memanggil MoveNext metode untuk memajukan enumerator ke elemen pertama koleksi sebelum membaca nilai Entry.
Properti Entry mengembalikan elemen yang sama hingga MoveNext metode atau Reset dipanggil. MoveNext Entry diatur ke elemen berikutnya.
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 mengembalikan false. Jika panggilan terakhir yang MoveNext dikembalikan false, Entry tidak ditentukan. Untuk mengatur Entry ke elemen pertama koleksi lagi, Anda dapat memanggil Reset diikuti dengan MoveNext.
Enumerator tetap valid selama koleksi tetap tidak berubah. Jika perubahan dilakukan pada koleksi, seperti menambahkan elemen atau mengubah kapasitas, enumerator tidak dapat divalidasi dan panggilan berikutnya ke MoveNext atau IEnumerator.Reset melempar InvalidOperationException.
.NET Core 3.0+ saja: Satu-satunya metode bermutasi yang tidak membatalkan enumerator adalah Remove dan Clear.
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.
Implementasi default koleksi di System.Collections.Generic namespace tidak disinkronkan.
Metode ini adalah operasi O(1).