C# - SortedList Class

The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.
A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.
The following table lists some of the commonly used properties of the SortedList class −
Capacity
Gets or sets the capacity of the SortedList.
Count
Gets the number of elements contained in the SortedList.
IsFixedSize
Gets a value indicating whether the SortedList has a fixed size.
IsReadOnly
Gets a value indicating whether the SortedList is read-only.
Item
Gets and sets the value associated with a specific key in the SortedList.
Keys
Gets the keys in the SortedList.
Values
Gets the values in the SortedList.
The following table lists some of the commonly used methods of the SortedList class −
public virtual void Add(object key, object value);
Adds an element with the specified key and value into the SortedList.
public virtual void Clear();
Removes all elements from the SortedList.
public virtual bool ContainsKey(object key);
Determines whether the SortedList contains a specific key.
public virtual bool ContainsValue(object value);
Determines whether the SortedList contains a specific value.
public virtual object GetByIndex(int index);
Gets the value at the specified index of the SortedList.
public virtual object GetKey(int index);
Gets the key at the specified index of the SortedList.
public virtual IList GetKeyList();
Gets the keys in the SortedList.
public virtual IList GetValueList();
Gets the values in the SortedList.
public virtual int IndexOfKey(object key);
Returns the zero-based index of the specified key in the SortedList.
public virtual int IndexOfValue(object value);
Returns the zero-based index of the first occurrence of the specified value in the SortedList.
public virtual void Remove(object key);
Removes the element with the specified key from the SortedList.
public virtual void RemoveAt(int index);
Removes the element at the specified index of SortedList.
public virtual void TrimToSize();
Sets the capacity to the actual number of elements in the SortedList.
The following example demonstrates the concept −
using System;
using System.Collections;

namespace CollectionsApplication {
   class Program {
      static void Main(string[] args) {
         SortedList sl = new SortedList();
         
         sl.Add("001", "Zara Ali");
         sl.Add("002", "Abida Rehman");
         sl.Add("003", "Joe Holzner");
         sl.Add("004", "Mausam Benazir Nur");
         sl.Add("005", "M. Amlan");
         sl.Add("006", "M. Arif");
         sl.Add("007", "Ritesh Saikia");
         
         if (sl.ContainsValue("Nuha Ali")) {
            Console.WriteLine("This student name is already in the list");
         } else {
            sl.Add("008", "Nuha Ali");
         }

         // get a collection of the keys. 
         ICollection key = sl.Keys;

         foreach (string k in key) {
            Console.WriteLine(k + ": " + sl[k]);
         }
      }
   }
}
When the above code is compiled and executed, it produces the following result −
001: Zara Ali
002: Abida Rehman
003: Joe Holzner
004: Mausam Banazir Nur
005: M. Amlan 
006: M. Arif
007: Ritesh Saikia
008: Nuha Ali


    119 Lectures 
     23.5 hours 

 Raja Biswas

    37 Lectures 
     13 hours 

 Trevoir Williams

    16 Lectures 
     1 hours 

 Peter Jepson

    159 Lectures 
     21.5 hours 

 Ebenezer Ogbu

    193 Lectures 
     17 hours 

 Arnold Higuit

    24 Lectures 
     2.5 hours 

 Eric Frick
 Print
 Add Notes
 Bookmark this page