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


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

Updated on: 05-Dec-2019

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements