Get or set the value associated with specified key in ListDictionary in C#

CsharpServer Side ProgrammingProgramming

To get or set the value associated with specified key in ListDictionary, the code is as follows −

Example

 Live Demo

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"]);
   }
}

Output

This will produce the following output −

Value associated with key C = Appliances

Example

Let us see another example −

 Live Demo

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"]);
   }
}

Output

This will produce the following output −

Value associated with key C = Appliances
Value associated with key C [Updated] = HDD
raja
Published on 05-Dec-2019 15:10:45
Advertisements