ハッシュテーブルC#のキーを含むICollectionを取得します
ハッシュテーブルのキーを含むICollectionを取得するには、コードは次のとおりです-
例
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable hash = new Hashtable();
hash.Add("A", "Electronics");
hash.Add("B", "Appliances");
hash.Add("C", "Pet Supplies");
hash.Add("D", "Books");
hash.Add("E", "Toys");
hash.Add("F", "Footwear");
hash.Add("G", "Clothing");
ICollection col = hash.Keys;
foreach(string str in col)
Console.WriteLine(str + ": " + hash[str]);
}
} 出力
これにより、次の出力が生成されます-
G: Clothing A: Electronics B: Appliances C: Pet Supplies D: Books E: Toys F: Footwear
例
別の例を見てみましょう-
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable hash = new Hashtable();
hash.Add(1, "AB");
hash.Add(2, "CD");
hash.Add(3, "EF");
hash.Add(4, "GH");
hash.Add(5, "IJ");
hash.Add(6, "KL");
hash.Add(7, "MN");
ICollection col = hash.Keys;
foreach(int str in col)
Console.WriteLine("Key = " + str + ", Value = " + hash[str]);
}
} 出力
これにより、次の出力が生成されます-
Key = 7, Value = MN Key = 6, Value = KL Key = 5, Value = IJ Key = 4, Value = GH Key = 3, Value = EF Key = 2, Value = CD Key = 1, Value = AB
-
C#のHashtableクラスのKeysプロパティとは何ですか?
ハッシュテーブルのキーを含むICollectionを取得します。コレクション内のすべてのキーが表示されます。以下のコードでは、すべてのキーを取得するために、ループを使用してコレクションをループしています。 foreach (int k in h.Keys) { Console.WriteLine(k); } 上記は、次のコードに示すようにすべてのキーを表示します- 例 using System; using System.Collections; class Program { static void Main() { &nb
-
辞書からキーのリストを取得するC#プログラム
辞書要素を設定する- Dictionary<int, string> d = new Dictionary<int, string>(); // dictionary elements d.Add(1, "One"); d.Add(2, "Two"); d.Add(3, "Three"); d.Add(4, "Four"); d.Add(5, "Five"); d.Add(6, "Six"); d.Add(7, "Seven");