在 C# 中獲取或設定 StringDictionary 中與指定鍵關聯的值


如下所示,獲取或設定 StringDictionary 中與指定鍵關聯的值的程式碼為 −

示例

 線上演示

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict = new StringDictionary ();
      strDict.Add("A", "Books");
      strDict.Add("B", "Electronics");
      strDict.Add("C", "Appliances");
      strDict.Add("D", "Pet Supplies");
      strDict.Add("E", "Clothing");
      strDict.Add("F", "Footwear");
      Console.WriteLine("Value associated with key D = "+strDict["D"]);
      Console.WriteLine("Value associated with key F = "+strDict["F"]);
   }
}

輸出

將產生以下輸出 −

Value associated with key D = Pet Supplies
Value associated with key F = Footwear

示例

讓我們看另一個示例 −

 線上演示

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict = new StringDictionary ();
      strDict.Add("A", "Books");
      strDict.Add("B", "Electronics");
      strDict.Add("C", "Appliances");
      strDict.Add("D", "Pet Supplies");
      strDict.Add("E", "Clothing");
      strDict.Add("F", "Footwear");
      Console.WriteLine("Value associated with key D = "+strDict["D"]);
      Console.WriteLine("Value associated with key F = "+strDict["F"]);
      strDict["F"] = "HDD";
      Console.WriteLine("Value associated with key F (UPDATED) = "+strDict["F"]);
   }
}

輸出

將產生以下輸出 −

Value associated with key D = Pet Supplies
Value associated with key F = Footwear
Value associated with key F (UPDATED) = HDD

更新於: 05-Dec-2019

66 次瀏覽

開啟 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.