Hashtable and SortedList in C#

Hashtables and SortedLists are two of the most commonly used collections in C#. Both of them belong to System.Collections namespace. Let us see what we can do with these collections.

Hashtable

A Hashtable stores collection of data in the form of key-value pair. This means that each item within the Hashtable consists of two parts: A key, and a value corresponding to that particular key. The items are accessed by passing key to the Hashtable variable. Have a look at the following example.

´╗┐using System;
using System.Collections;

namespace CSharpTutorials {
    class Tutorial {
        static void Main() {
            // Creating a Hashtable
            Hashtable sampletable = new Hashtable();

            // Adding elements to hashtable 
            sampletable.Add("Jacob","12335");
            sampletable.Add("Stacy", "43434");
            sampletable.Add("Kelly", "56332");
            sampletable.Add("Mike", "23413");
            sampletable.Add("Carla", "83578");
            sampletable.Add("Vinset", "67393");

            // Printing phone number of kelly using key.
            Console.WriteLine("Phone number of Kelly is:" + sampletable["Kelly"]);

            //Checking if Mike exists in the hashtable
            Console.WriteLine("If Mike exists?:" + sampletable.ContainsKey("Mike"));

            // Removing Element from hashtable
            sampletable.Remove("Carla");
            Console.ReadKey();
        } 
    }
}

Download the code

In the above example a Hashtable named sampletable has been declared. Some items have been added in this hashtable in the form of key-value pairs via add function. The key refers to name of some random person, while value refers to imaginary phone number. You can have any key-value pair that you like. Now to access number of some person, simply use its key and pass it to the hashtable as index. Above code also explains the usage of ContainsKey and Remove methods.

SortedList

SortedList in C# is a collection that contains properties of both an ArrayList class and a Hashtable. Elements within a SortedList are stored in the form of Key-value pairs and are sorted by default on the basis of key. The elements can be accessed both by index and by key. Have a look at this example.

´╗┐using System;
using System.Collections;

namespace CSharpTutorials {
    class Tutorial {
        static void Main() {

            SortedList sortedlist = new SortedList();

            sortedlist.Add("John", "76489");
            sortedlist.Add("Dave", "67849");
            sortedlist.Add("Mike", "68699");
            sortedlist.Add("Sara", "76233");
            sortedlist.Add("Mark", "56739");

            //printing SortedList elements
            foreach(var a in sortedlist.Keys)  Console.WriteLine(a);

            //Accessing elements via index
            Console.WriteLine("Value of Dave is:" + sortedlist.GetByIndex(0));

            //Accessing elements via key
            Console.WriteLine("Value of Dave is:" + sortedlist["Dave"]);

            Console.ReadKey();
        } 
    }
}

Download the code

In the above code a SortedList has been defined and iterated. The iterated collection will display the items i alphabetical order which means that SortedList is sorted by default. Next, a value in the list is accessed first via index and then via key.

<<< Reflections in C#Stacks & Queues in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .