在 C# 中获取或设置 SortedList 中指定键关联的值

csharpserver side programmingprogramming更新于 2025/5/21 2:07:17

要获取或设置 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

相关文章