Gets or sets the value at the specified key in StringDictionary in C#


To get or set the value at the specified key in StringDictionary, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary myDict = new StringDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      myDict.Add("6", "Ultrabook");
      myDict.Add("7", "HDD");
      myDict.Add("8", "SDD");
      myDict.Add("9", "Headphone");
      myDict.Add("10", "Earphone");
      Console.WriteLine("Value for key 5 = "+myDict["5"]);
   }
}

Output

This will produce the following output −

Value for key 5 = Notebook

Example

Let us see another example −

 Live Demo

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary myDict = new StringDictionary();
      myDict.Add("1", "Tablet");
      myDict.Add("2", "Desktop");
      myDict.Add("3", "Speakers");
      myDict.Add("4", "Laptop");
      myDict.Add("5", "Notebook");
      myDict.Add("6", "Ultrabook");
      myDict.Add("7", "HDD");
      myDict.Add("8", "SDD");
      myDict.Add("9", "Headphone");
      myDict.Add("10", "Earphone");
      Console.WriteLine("Value for key 5 = "+myDict["5"]);
      myDict["5"] = "Flash Drive";
      Console.WriteLine("Value for key 5 [Updated] = "+myDict["5"]);
   }
}

Output

This will produce the following output −

Value for key 5 = Notebook
Value for key 5 [Updated] = Flash Drive

Updated on: 10-Dec-2019

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements