Bagikan melalui


SortedList<TKey,TValue>.IDictionary.GetEnumerator Metode

Definisi

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 daftar yang diurutkan dengan menggunakan foreach pernyataan (For Each di Visual Basic, for each di C++), yang menyembunyikan penggunaan enumerator. Secara khusus, perhatikan bahwa enumerator untuk System.Collections.IDictionary antarmuka mengembalikan DictionaryEntry objek daripada KeyValuePair<TKey,TValue> objek.

Contoh kode adalah bagian dari contoh yang lebih besar, termasuk output, yang disediakan untuk metode .IDictionary.Add

using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedList<string, string>();

        // Add some elements to the sorted list. 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");
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main() 

        ' Create a new sorted list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of String, String)
        
        ' Add some elements to the sorted list. 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 sorted list elements
// with the IDictionary interface, the elements are retrieved
// as DictionaryEntry objects instead of KeyValuePair objects.
Console.WriteLine();
foreach( DictionaryEntry de in openWith )
{
    Console.WriteLine("Key = {0}, Value = {1}",
        de.Key, de.Value);
}
' When you use foreach to enumerate sorted list 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

Pernyataan foreach bahasa C# (for each dalam C++, For Each di Visual Basic) menyembunyikan kompleksitas enumerator. Oleh karena itu, penggunaan foreach disarankan, alih-alih secara 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. Reset juga membawa enumerator kembali ke posisi ini. Pada posisi ini, Entry tidak terdefinisi. Oleh karena itu, Anda harus memanggil MoveNext untuk memajukan enumerator ke elemen pertama koleksi sebelum membaca nilai Entry.

Entry mengembalikan objek yang sama hingga atau MoveNextReset dipanggil. MoveNextEntry mengatur 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 untuk 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, memodifikasi, atau menghapus elemen, enumerator tidak valid dan panggilan berikutnya ke MoveNext atau Reset melemparkan InvalidOperationException.

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 tidak disinkronkan System.Collections.Generic .

Metode ini adalah operasi O(1).

Berlaku untuk

Lihat juga