Easy Tutorial
❮ Csharp Arraylist Csharp Operator Overloading ❯
# C# `SortedList`

[C# Collections](csharp-collection.html)

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

A SortedList is a combination of an array and a hashtable. It contains a list 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 items in the collection are always sorted by the key value.

## Methods and Properties of the SortedList Class

The following table lists some commonly used **properties** of the SortedList class:

| Property | Description |
| --- | --- |
| Capacity | Gets or sets the capacity of the SortedList. |
| Count | Gets the number of elements 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 or 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 commonly used **methods** of the SortedList class:

| No. | Method & Description |
| --- | --- |
| 1 | public virtual void Add(<br> object key,<br> object value<br>); <br>Adds an element with the specified key and value into the SortedList. |
| 2 | public virtual void Clear(); <br>Removes all elements from the SortedList. |
| 3 | public virtual bool ContainsKey(<br> object key<br>); <br>Determines whether the SortedList contains a specific key. |
| 4 | public virtual bool ContainsValue(<br> object value<br>); <br>Determines whether the SortedList contains a specific value. |
| 5 | public virtual object GetByIndex(<br> int index<br>); <br>Gets the value at the specified index of the SortedList. |
| 6 | public virtual object GetKey(<br> int index<br>); <br>Gets the key at the specified index of the SortedList. |
| 7 | public virtual IList GetKeyList(); <br>Gets the keys in the SortedList. |
| 8 | public virtual IList GetValueList(); <br>Gets the values in the SortedList. |
| 9 | public virtual int IndexOfKey(<br> object key<br>); <br>Returns the zero-based index of the specified key in the SortedList. |
| 10 | public virtual int IndexOfValue(<br> object value<br>); <br>Returns the zero-based index of the first occurrence of the specified value in the SortedList. |
| 11 | public virtual void Remove(<br> object key<br>); <br>Removes the element with the specified key from the SortedList. |
| 12 | public virtual void RemoveAt(<br> int index<br>); <br>Removes the element at the specified index of the SortedList. |
| 13 | public virtual void TrimToSize(); <br>Sets the capacity to the actual number of elements in the SortedList. |

## Example

The following example demonstrates the concept of SortedList:

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

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 the collection of 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 ```

C# Collection

❮ Csharp Arraylist Csharp Operator Overloading ❯