獲取或設定 Hashtable 中與指定鍵關聯的值


要獲取或設定 Hashtable 中與指定鍵關聯的值,程式碼如下:

示例

 演示

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("1", "AB");
      hash.Add("2", "BC");
      hash.Add("3", "DE");
      hash.Add("4", "EF");
      hash.Add("5", "GH");
      hash.Add("6", "IJ");
      hash.Add("7", "KL");
      hash.Add("8", "MN");
      hash.Add("9", "OP");
      hash.Add("10", "QR");
      Console.WriteLine("Value at key 3 = "+hash["3"]);
   }
}

輸出

這將產生以下輸出:

Value at key 3 = DE

示例

讓我們看另一個示例:

 演示

using System;
using System.Collections;
public class Demo {
   public static void Main() {
      Hashtable hash = new Hashtable();
      hash.Add("1", "AB");
      hash.Add("2", "BC");
      hash.Add("3", "DE");
      hash.Add("4", "EF");
      hash.Add("5", "GH");
      hash.Add("6", "IJ");
      hash.Add("7", "KL");
      hash.Add("8", "MN");
      hash.Add("9", "OP");
      hash.Add("10", "QR");
      Console.WriteLine("Value at key 3 = "+hash["3"]);
      hash["3"] = "UV";
      Console.WriteLine("Value at key 3 (UPDATED) = "+hash["3"]);
   }
}

輸出

這將產生以下輸出:

Value at key 3 = DE
Value at key 3 (UPDATED) = UV

更新時間: 2019 年 12 月 10 日

135 次瀏覽

為你的職業生涯加油

透過完成此課程來獲得認證

開始
廣告
© . All rights reserved.