在 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 次浏览

开启 职业生涯

完成课程获得认证

开始学习
广告