獲取或設定與 C# 中 ListDictionary 中指定鍵相關聯的值


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

示例

 即時演示

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

輸出

這將產生以下輸出 −

Value associated with key C = Appliances

示例

讓我們看另一個示例 −

 即時演示

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

輸出

這將產生以下輸出 −

Value associated with key C = Appliances
Value associated with key C [Updated] = HDD

更新時間: 05-12-2019

93 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

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