HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

SortedList Collection Type
SortedList is a collection class prsent in System.Collections namespace. The data in a SortedList is stored as key-value pairs. For example if we have a list of PersonId's and Names. PersonId's can be stored as a keys and Names can be stored as values. So each element in the SortedList will have the personID as the key and Name as value.
The following example shows how to create and initialize a SortedList and how to print out its keys and values. In the example below
  • Items are added to the SortedList with the Add() method.
  • Count property determines the number of elements contained in the SortedList.
  • Capacity property determines the number of elements that the SortedList can contain.If the number of elements added to the list reaches the current capacity, the capacity is automatically doubled.
  • TrimToSize() method is used to size a SortedList to its final size.
  • DictionaryEntry is a struct whcih can hold a HashTable elements key and Value. In our example we are using DictonaryEntry to loop thru the HashTable.
  • A SortedList object automatically sort the items in alphabetic or numeric order.
  • SortedList is unique among all Collections classes in that each element can be accessed three ways: using the key, the value, or the index.
  • Use SortedList if you want a collection that holds key-and-value pairs but also need the flexibility of an indexed list.

using System;

using System.Collections;

class SortedListDemo

{

    public static void Main()

    {

        //Create an instance of SortedList

        SortedList SL = new SortedList();

        //Add elements(Key-Value pairs) to the SortedList

        SL.Add(1, "Prasad");

        SL.Add(2, "Giri");

        SL.Add(3, "Ravi");

        //Loop thru each DictonaryEntry and print key and value of each element

        foreach (DictionaryEntry DE in SL)

        {

            Console.WriteLine("Key = " + DE.Key.ToString() + " Value = " + DE.Value.ToString());

        }

        //determines the number of elements contained in the SortedList

        Console.WriteLine("Count = " + SL.Count);

        //Determines the capacity of a SortedList

        Console.WriteLine("Capcity = " + SL.Capacity);

        //Trims the size of the SortedList to the actual number of elements present

        SL.TrimToSize();

        Console.WriteLine("After trimming, Capcity = " + SL.Capacity);

    }

}

In the example below
  • We retrieve the Name of the Person whose key is 2
        Console.WriteLine(SL[2]);
  • We also set the value to Giridhar using the key
        SL[2] = "GiriDhar";

using System;

using System.Collections;

class SortedListDemo

{

    public static void Main()

    {

        //Create an instance of SortedList

        SortedList SL = new SortedList();

        //Add elements(Key-Value pairs) to the SortedList

        SL.Add(1, "Prasad");

        SL.Add(2, "Giri");

        SL.Add(3, "Ravi");

        //Print the Person Name with PersonID 2

        Console.WriteLine(SL[2]);

        //Change the Person Name from Giri to GiriDhar

        SL[2] = "GiriDhar";

        Console.WriteLine(SL[2]);

    }

}

Other SortedList useful methods with an example
  • ContainsKey()
  • ContainsValue()
  • Remove()
  • Clear()

using System;

using System.Collections;

class SortedListDemo

{

    public static void Main()

    {

        //Create an instance of SortedList

        SortedList SL = new SortedList();

        //Add elements(Key-Value pairs) to the SortedList

        SL.Add(1, "Prasad");

        SL.Add(2, "Giri");

        SL.Add(3, "Ravi");

 

        //Determines whether the SortedList contains a specific key

        Console.WriteLine("Is key 2 Present:" + SL.ContainsKey(2).ToString());

        //Determines whether the SortedList contains a specific value

        Console.WriteLine("Is value Giri Present:" + SL.ContainsValue("Giri"));

        //Removes the element with the specified key from the SortedList

        SL.Remove(2);

        //Removes all elements from the SortedList.

        SL.Clear();

    }

}

Other related articles

Njoy Programming
   ByPrasad Cherukuri