SortedDictionary.System.Collections.IDictionary.GetEnumerator メソッド
アセンブリ: System (system.dll 内)

Private Function System.Collections.IDictionary.GetEnumerator As IDictionaryEnumerator Implements IDictionary.GetEnumerator
Dim instance As SortedDictionary(Of TKey, TValue) Dim returnValue As IDictionaryEnumerator returnValue = CType(instance, 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 を呼び出します。
コレクションが変更されない限り、列挙子は有効なままです。要素の追加、変更、削除などの変更がコレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作は未定義になります。
列挙子はコレクションへの排他アクセス権を持たないため、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションをロックすることにより、列挙処理でのスレッド セーフを確保できます。コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

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 sorted dictionary of strings, with string keys , ' and access it using the IDictionary interface. ' Dim openWith As IDictionary = _ New SortedDictionary(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 sorted dictionary of strings, with string keys , // and access it using the IDictionary interface. // IDictionary openWith = new SortedDictionary<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 /> } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SortedDictionary.System.Collections.IDictionary.GetEnumerator メソッドのページへのリンク