C#でStringDictionaryを反復処理する列挙子を取得する方法
C#のStringDictionaryを反復処理するための列挙子(Enumerator)を取得するには、GetEnumerator()メソッドを使用します。このメソッドはIEnumeratorを返し、これを使ってコレクション内のキーと値のペアを順番に取り出すことができます。
列挙子から取得できる各要素はDictionaryEntry型にキャストすることで、KeyプロパティとValueプロパティにアクセスできます。
例1:GetEnumerator()を使った基本的な反復処理
以下のコードでは、2つのStringDictionaryを作成し、1つ目はforeach文で、2つ目はGetEnumerator()で取得した列挙子を使って反復処理しています。
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 key-value pairs...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}出力結果
上記のコードを実行すると、次のような出力が得られます。
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad StringDictionary2 key-value pairs... Key = 1, Value = A Key = 2, Value = B Key = 3, Value = C Key = 4, Value = D Key = 5, Value = E
なお、StringDictionaryはキーを小文字に変換して格納するため、追加時に大文字で指定した「A」〜「G」が出力では「a」〜「g」と表示されている点に注意してください。
例2:whileループとMoveNext()による反復処理
もう一つの例を見てみましょう。ここでは、MoveNext()メソッドで次の要素へ移動しながら、Currentプロパティで現在の要素を取得しています。
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "One");
strDict.Add("2", "Two");
strDict.Add("3", "Three");
strDict.Add("4", "Four");
Console.WriteLine("StringDictionary key-value pairs...");
IEnumerator demoEnum = strDict.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}出力結果
上記のコードを実行すると、次のような出力が得られます。
StringDictionary key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four
ポイントまとめ
GetEnumerator()メソッドは、StringDictionaryを反復処理するためのIEnumeratorオブジェクトを返します。MoveNext()で次の要素に移動し、要素が存在すればtrueを返します。Currentプロパティで現在の要素を取得し、DictionaryEntryにキャストしてキーと値にアクセスします。- StringDictionaryはキーを小文字化して扱うため、大文字のキーを追加しても小文字で格納されます。
-
C#でDictionaryを反復処理する列挙子を取得する方法(GetEnumeratorの使い方)
C#のDictionary<TKey, TValue>クラスでは、GetEnumerator()メソッドを呼び出すことで、ディクショナリ内のキーと値のペアを順番に走査する列挙子(Enumerator)を取得できます。戻り値はIDictionaryEnumerator型として扱うことができ、MoveNext()メソッドで次の要素へ移動しながら、KeyプロパティとValueプロパティでそれぞれキーと値を読み取ります。 例1:int型のキーを持つDictionaryの場合 まずは、キーがint型・値がstring型のディクショナリを作成し、列挙子を使って全要素を表示する基本例を見てみま
-
C#でStringCollectionを反復処理する列挙子(Enumerator)を取得する方法
StringCollectionを反復処理するための列挙子(Enumerator)を取得するには、GetEnumerator()メソッドを使用します。このメソッドはStringEnumerator型のオブジェクトを返し、MoveNext()とCurrentを組み合わせることで、コレクション内の各要素に順番にアクセスできます。基本的な使い方まずは、配列の要素をStringCollectionに追加し、列挙子を使って反復処理する例を見てみましょう。using System; using System.Collections.Specialized; public class Demo { &nbs