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


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

示例

 即時演示

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

輸出

這將產生以下輸出 −

Value associated with key E = Clothing
Value associated with key E [Updated] = HDD

示例

我們來看另一個示例 −

 即時演示

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

輸出

這將產生以下輸出 −

Value associated with key D = Pet Supplies

更新於:05-12-2019

86 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.