SortedList with Examples in C#?

CsharpServer Side ProgrammingProgramming

The SortedList class in C# represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.

Following are the properties of the SortedList class −

Sr.NoProperty & Description
1Capacity
Gets or sets the capacity of a SortedList object.
2Count
Gets the number of elements contained in a SortedList object.
3IsFixedSize
Gets a value indicating whether a SortedList object has a fixed size.
4IsReadOnly
Gets a value indicating whether a SortedList object is readonly.
5IsSynchronized
Gets a value indicating whether access to a SortedList object is synchronized (thread safe).
6Item[Object]
Gets or sets the value associated with a specific key in a SortedList object.
7Keys
Gets the keys in a SortedList object.
8SyncRoot
Gets an object that can be used to synchronize access to a SortedList object.
9Values
Gets the values in a SortedList object.

Following are some of the methods of the Sorted class −

Sr.NoMethods & Description
1Add(Object, Object)
Adds an element with the specified key and value to a SortedList object.
2Clear()
Removes all elements from a SortedList object.
3Clone()
Creates a shallow copy of a SortedList object.
4Contains(Object)
Determines whether a SortedList object contains a specific key.
5ContainsKey(Object)
Determines whether a SortedList object contains a specific key.
6ContainsValue(Object)
Determines whether a SortedList object contains a specific value.
7CopyTo(Array, Int32)
Copies SortedList elements to a onedimensional Array object, starting at the specified index in the array.
8SyncRoot
Gets an object that can be used to synchronize access to a SortedList object.
9Values
Gets the values in a SortedList object.

Let us now see some examples −

To get the number of elements contained in the SortedList, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList sortedList = new SortedList();
      sortedList.Add("A", "1");
      sortedList.Add("B", "2");
      sortedList.Add("C", "3");
      sortedList.Add("D", "4");
      sortedList.Add("E", "5");
      sortedList.Add("F", "6");
      sortedList.Add("G", "7");
      sortedList.Add("H", "8");
      sortedList.Add("I", "9");
      sortedList.Add("J", "10");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in sortedList) {
         Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
      sortedList.Clear();
      Console.WriteLine("Count of SortedList (updated) = "+sortedList.Count);
   }
}

Output

This will produce the following output −

SortedList elements...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Key = G, Value = 7
Key = H, Value = 8
Key = I, Value = 9
Key = J, Value = 10
Count of SortedList key-value pairs = 10
Count of SortedList (updated) = 0

To check if two SortedList objects are equal, the code is as follows −

Example

 Live Demo

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList list1 = new SortedList();
      list1.Add("One", 1);
      list1.Add("Two ", 2);
      list1.Add("Three ", 3);
      list1.Add("Four", 4);
      list1.Add("Five", 5);
      list1.Add("Six", 6);
      list1.Add("Seven ", 7);
      list1.Add("Eight ", 8);
      list1.Add("Nine", 9);
      list1.Add("Ten", 10);
      Console.WriteLine("SortedList1 elements...");
      foreach(DictionaryEntry d in list1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      SortedList list2 = new SortedList();
      list2.Add("A", "Accessories");
      list2.Add("B", "Books");
      list2.Add("C", "Smart Wearable Tech");
      list2.Add("D", "Home Appliances");
      Console.WriteLine("\nSortedList2 elements...");
      foreach(DictionaryEntry d in list2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      SortedList list3 = new SortedList();
      list3 = list2;
      Console.WriteLine("\nIs SortedList2 equal to SortedList3? = "+list3.Equals(list2));
   }
}

Output

This will produce the following output −

SortedList1 elements...
Eight  8
Five 5
Four 4
Nine 9
One 1
Seven  7
Six 6
Ten 10
Three  3
Two  2

SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
Is SortedList2 equal to SortedList3? = True
raja
Published on 16-Dec-2019 14:08:11
Advertisements