SortedList<TKey,TValue>.IDictionary.GetEnumerator 方法

定义

 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

返回

IDictionaryEnumerator

IDictionaryEnumerator 的一个 IDictionary

实现

示例

下面的代码示例演示如何使用 foreach Visual Basic C++) for each中的语句 (For Each来枚举排序列表中的键/值对,从而隐藏枚举器的用法。 具体而言,请注意,接口的 System.Collections.IDictionary 枚举器返回 DictionaryEntry 对象而不是 KeyValuePair<TKey,TValue> 对象。

代码示例是为该方法提供的大型示例的一部分,包括输出 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

注解

foreach C++ 语言 (for each的语句, For Each Visual Basic) 隐藏枚举器的复杂性。 因此,建议使用 foreach,而不是直接操作枚举数。

枚举器可用于读取集合中的数据,但不能用于修改基础集合。

最初,枚举数定位在集合中第一个元素的前面。 Reset 也会将枚举器放回此位置。 在此位置上,未定义 Entry。 因此,在读取 MoveNext 的值之前,必须调用 Entry 将枚举器向前移动到集合的第一个元素。

在调用 EntryMoveNext 之前,Reset 返回同一对象。 MoveNextEntry 设置为下一个元素。

如果 MoveNext 传递集合的末尾,则枚举器将定位在集合中的最后一个元素之后并 MoveNext 返回 false。 当枚举器处于此位置时,后续调用 MoveNext 也会返回 false。 如果返回false的最后一次调用MoveNextEntry则为未定义。 若要再次将 Entry 设置为集合的第一个元素,可以调用 Reset 并接着调用 MoveNext

只要集合保持不变,枚举器就仍有效。 如果对集合进行了更改(例如添加、修改或删除元素),则枚举器将不可恢复地失效,下次调用 MoveNextReset 引发该 InvalidOperationException调用。

枚举数没有对集合的独占访问权;因此,从头到尾对一个集合进行枚举在本质上不是一个线程安全的过程。 若要确保枚举过程中的线程安全性,可以在整个枚举过程中锁定集合。 若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

集合的默认 System.Collections.Generic 实现不会同步。

此方法是一个 O (1) 操作。

适用于

另请参阅