C#でHybridDictionaryを反復処理する列挙子を取得する方法
HybridDictionaryは、要素数が少ないうちはListDictionaryとして動作し、要素が増えると自動的にHashtableへ切り替わるコレクションです。このHybridDictionaryを反復処理するための列挙子を取得するには、GetEnumerator()メソッドを使用します。
GetEnumerator()メソッドの仕組み
GetEnumerator()メソッドを呼び出すと、IDictionaryEnumerator型の列挙子が返されます。この列挙子のMoveNext()メソッドを呼び出すたびにコレクション内の次の要素へ移動し、KeyプロパティとValueプロパティから、現在の要素のキーと値をそれぞれ取得できます。
例:GetEnumerator()でキーと値のペアを取得する
次の例では、まず1つ目のHybridDictionaryをforeachループで反復処理して表示し、続いて2つ目のHybridDictionaryを、GetEnumerator()メソッドで取得した列挙子を使って反復処理しています。
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict1 = new HybridDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("HybridDictionary1 elements...");
foreach(DictionaryEntry d in dict1){
Console.WriteLine(d.Key + " " + d.Value);
}
HybridDictionary dict2 = new HybridDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nHybridDictionary2 key-value pairs...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
出力
上記のコードを実行すると、次のような結果が得られます。
HybridDictionary1 elements... A Books B Electronics C Smart Wearables D Pet Supplies E Clothing F Footwear HybridDictionary2 key-value pairs... Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six
別の例:文字列データでの反復処理
今度は名前のデータを格納したHybridDictionaryに対して、同じくGetEnumerator()メソッドで列挙子を取得し、whileループですべての要素を表示してみましょう。
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
HybridDictionary dict = new HybridDictionary();
dict.Add("A", "Gary");
dict.Add("B", "Andy");
dict.Add("C", "Mark");
dict.Add("D", "Barry");
dict.Add("E", "Katie");
dict.Add("F", "John");
Console.WriteLine("HybridDictionary key-value pairs...");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}
出力
上記のコードを実行すると、次のような結果が得られます。
HybridDictionary key-value pairs... Key = A, Value = Gary Key = B, Value = Andy Key = C, Value = Mark Key = D, Value = Barry Key = E, Value = Katie Key = F, Value = John
まとめ
HybridDictionaryを反復処理するには、GetEnumerator()メソッドでIDictionaryEnumeratorを取得し、MoveNext()がtrueを返す間、KeyプロパティとValueプロパティを読み取るのが基本の流れです。なお、単純にすべての要素を表示したいだけであれば、例1のようにforeachループを使っても同じように反復処理できます。
-
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