在 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

更新於:2019 年 12 月 10 日

79 次瀏覽

開啟你的 事業

完成課程,獲取認證

開始學習
廣告