SortedList Class:-
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.


Methods and Properties of the SortedList Class:-

Methods of SortedList class:-

Sr.No. Methods
1 public virtual void Add(object key, object value);

Adds an element with the specified key and value into the SortedList.

2 public virtual void Clear();

Removes all elements from the SortedList.

3 public virtual bool ContainsKey(object key);

Determines whether the SortedList contains a specific key.

4 public virtual bool ContainsValue(object value);

Determines whether the SortedList contains a specific value.

5 public virtual object GetByIndex(int index);

Gets the value at the specified index of the SortedList.

6 public virtual object GetKey(int index);

Gets the key at the specified index of the SortedList.

7 public virtual IList GetKeyList();

Gets the keys in the SortedList.

8 public virtual IList GetValueList();

Gets the values in the SortedList.

9 public virtual int IndexOfKey(object key);

Returns the zero-based index of the specified key in the SortedList.

10 public virtual int IndexOfValue(object value);

Returns the zero-based index of the first occurrence of the specified value in the SortedList.

11 public virtual void Remove(object key);

Removes the element with the specified key from the SortedList.

12 public virtual void RemoveAt(int index);

Removes the element at the specified index of SortedList.

13 public virtual void TrimToSize();

Sets the capacity to the actual number of elements in the SortedList.



Properties of SortedList class:-

Property Description
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.


Example of Sorted List class:-


using System;
using System.Collections;
class SortedListDemo
{
    public SortedListDemo()
    {
        Console.WriteLine("This is SortedList demo::\n");
    }
    public void Show()
    {
        SortedList sl = new SortedList();

        sl.Add("001", "Santosh kumar singh");
        sl.Add("002", "Gagan");
        sl.Add("003", "Manorma arya");
        sl.Add("004", "Reena kumari");
        sl.Add("005", "Anjanee kumar singh");
        sl.Add("006", "Pramod kumar sah");
        sl.Add("007", "Laxmi Pati Ojha");
        sl.Add("008", "Chandan kumar");

        if (sl.ContainsValue("Rajeev kumar"))
        {
            Console.WriteLine("This student name is already in the list");
        }
        else
        {
            sl.Add("009", "Rajeev kumar");
        }

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

        foreach (string k in key)
        {
            Console.WriteLine(k + ": " + sl[k]);
        }
    }
}


class Test
{
    static void Main(string[] args)
    {       
        SortedListDemo objSL = new SortedListDemo();
        objSL.Show();
        Console.ReadLine();
    }
}