Dictionary.System.Collections.IDictionary.GetEnumerator メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Dictionary.System.Collections.IDictionary.GetEnumerator メソッドの意味・解説 

Dictionary.System.Collections.IDictionary.GetEnumerator メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

IDictionary の IDictionaryEnumerator を返します。

名前空間: System.Collections.Generic
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Private Function System.Collections.IDictionary.GetEnumerator
 As IDictionaryEnumerator Implements IDictionary.GetEnumerator
Dim instance As Dictionary(Of
 TKey, TValue)
Dim returnValue As IDictionaryEnumerator

returnValue = CType(instance, IDictionary).GetEnumerator
IDictionaryEnumerator IDictionary.GetEnumerator ()
private:
virtual IDictionaryEnumerator^ System.Collections.IDictionary.GetEnumerator () sealed
 = IDictionary::GetEnumerator

戻り値
IDictionary の IDictionaryEnumerator。

解説解説

列挙処理のために、各アイテムは、値とそのキーを表す KeyValuePair 構造体です。

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

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

最初に、列挙子はコレクションの最初の要素の前に配置されます。また、Reset メソッドは、列挙子を最初の位置に戻します。この位置で、Entry プロパティが未定義です。したがって、Entry の値を読み取る前に、MoveNext メソッドを呼び出して、コレクションの最初の要素に列挙子を進める必要があります。

Entry プロパティは、MoveNext メソッドまたは Reset メソッドが呼び出されるまでは同じ要素を返します。MoveNext は、Entry を次の要素に設定します。

MoveNext がコレクションの末尾を過ぎると、列挙子はコレクションの最後の要素の後ろに配置され、MoveNext は false を返します。列挙子がこの位置にある場合、以降、MoveNext を呼び出しても false が返されます。MoveNext への最後の呼び出しで false が返された場合は、Entry が未定義です。Entry をコレクションの最初の要素に再び設定するには、Reset を呼び出してから、MoveNext を呼び出します。

コレクションが変更されない限り、列挙子は有効なままです。要素の追加、変更、削除などの変更がコレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作は未定義になります。

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

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

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

使用例使用例

foreach ステートメント (Visual Basic の場合は For Each、C++ の場合は for each) を使用してキー/値ペアを列挙する方法を次のコード例に示します。このステートメントを使用すると、列挙子を使用していることが隠されます。特に、System.Collections.IDictionary インターフェイスの列挙子が KeyValuePair オブジェクトではなく DictionaryEntry オブジェクトを返すことに注意してください。

次のコード例は、System.Collections.IDictionary.Add メソッドのトピックで取り上げている例および出力の一部分です。

Imports System
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")
<br /><span space="preserve">...</span><br
 />        ' 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 
<br /><span space="preserve">...</span><br
 />
    End Sub

End Class
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");
<br /><span space="preserve">...</span><br /> 
       // 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();
        foreach( DictionaryEntry de in openWith
 )
        {
            Console.WriteLine("Key = {0}, Value = {1}", 
                de.Key, de.Value);
        }
<br /><span space="preserve">...</span><br /> 
   }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

Dictionary.System.Collections.IDictionary.GetEnumerator メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Dictionary.System.Collections.IDictionary.GetEnumerator メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS