獲取或設定 C# 中 StringDictionary 中指定鍵的值


要獲取或設定 StringDictionary 中指定鍵的值,程式碼如下 −

示例

 線上演示

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary myDict = new StringDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      myDict.Add("6", "Ultrabook");
      myDict.Add("7", "HDD");
      myDict.Add("8", "SDD");
      myDict.Add("9", "Headphone");
      myDict.Add("10", "Earphone");
      Console.WriteLine("Value for key 5 = "+myDict["5"]);
   }
}

輸出

會產生以下輸出 −

Value for key 5 = Notebook

示例

再讓我們看另一個示例 −

 線上演示

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary myDict = new StringDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      myDict.Add("6", "Ultrabook");
      myDict.Add("7", "HDD");
      myDict.Add("8", "SDD");
      myDict.Add("9", "Headphone");
      myDict.Add("10", "Earphone");
      Console.WriteLine("Value for key 5 = "+myDict["5"]);
      myDict["5"] = "Flash Drive";
      Console.WriteLine("Value for key 5 [Updated] = "+myDict["5"]);
   }
}

輸出

會產生以下輸出 −

Value for key 5 = Notebook
Value for key 5 [Updated] = Flash Drive

更新時間:2019 年 12 月 10 日

89 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.