SortedList<TKey,TValue>.IDictionary.GetEnumerator メソッド

定義

IDictionaryEnumeratorIDictionary を返します。

 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

IDictionaryEnumeratorIDictionary

実装

次のコード例は、列挙子の使用を非表示にするステートメント (For EachVisual Basic、C++ では) を使用foreachして、for each並べ替えられたリスト内のキーと値のペアを列挙する方法を示しています。 特に、インターフェイスの列挙子はオブジェクトではなくKeyValuePair<TKey,TValue>オブジェクトをSystem.Collections.IDictionaryDictionaryEntry返します。

コード例は、メソッドに対して提供される出力を含む、より大きな例の 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

注釈

C# 言語の foreach ステートメント (C++ では for each、Visual Basic では For Each) では、列挙子の複雑さが隠されています。 したがって、列挙子を直接操作するのではなく、foreach を使用することをお勧めします。

列挙子を使用すると、コレクション内のデータを読み取ることができますが、基になるコレクションを変更することはできません。

最初、列挙子はコレクションの先頭の要素の前に位置付けられます。 また、Reset メソッドは、列挙子を最初の位置に戻します。 この位置では、Entry が未定義です。 そのため、MoveNext の値を読み取る前に、Entry を呼び出して列挙子をコレクションの最初の要素に進める必要があります。

Entry は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。 MoveNext は、Entry を次の要素に進めます。

MoveNext がコレクションの末尾を通過した場合、列挙子がコレクション内の最後の要素の後に配置され、MoveNextfalse を返します。 列挙子がこの位置にある場合、後続の MoveNext 呼び出しも false を返します。 返されるfalseEntry最後の呼び出しMoveNextが未定義の場合。 コレクションの先頭の要素に再び Entry を設定するには、先に Reset を呼び出してから MoveNext を呼び出します。

列挙子は、コレクションが変更されない限り有効です。 要素の追加、変更、削除など、コレクションに変更が加えられた場合、列挙子は回復不能に無効になり、次の呼び出し MoveNext または Reset スロー InvalidOperationExceptionが行われます。

列挙子はコレクションに排他アクセスできないため、コレクションの列挙処理は本質的にスレッド セーフな処理ではありません。 列挙処理でスレッド セーフを確保するには、列挙処理が終わるまでコレクションをロックできます。 コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

コレクションの System.Collections.Generic 既定の実装は同期されません。

このメソッドは、O(1) 操作です。

適用対象

こちらもご覧ください