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

CsharpServer Side ProgrammingProgramming

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

Example

 Live Demo

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

Output

This will produce the following output −

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

Example

Let us see another example −

 Live Demo

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

Output

This will produce the following output −

Value associated with key D = Pet Supplies
raja
Published on 05-Dec-2019 10:55:14
Advertisements