HashTable in C#

  • Hashtable stores elements in a Key/Value pair format and the elements are organized based on the hash code of the key.
  • In a Key/Value pair, Key should not be null, but a value can be null. Also Key should be unique, but value can be duplicate.
  • The Objects used as key are required to override GetHashCode and Equals methods of base class Object.
  • Like ArrayList, HashTable also have overhead of boxing and unboxing of objects while adding or accessing elements from HashTable.

See below example of using HashTable.

+
Code
using System;
using System.Collections;
 
namespace HashTable_Collection
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp1 = new Employee() { empId = 111,empName="Sam"};
            Employee emp2 = new Employee() { empId = 112, empName = "Jane" };
            Employee emp3 = new Employee() { empId = 113, empName = "Sachin" };
            Employee emp4 = new Employee() { empId = 114, empName = "Jack" };
 
            Hashtable hashTblEmployee = new Hashtable();
            hashTblEmployee.Add(emp1.empId, emp1.empName);
            hashTblEmployee.Add(emp2.empId, emp2.empName);
            hashTblEmployee.Add(emp3.empId, emp3.empName);
            hashTblEmployee.Add(emp4.empId, emp4.empName);
 
            foreach (DictionaryEntry kp in hashTblEmployee)
            {
                Console.WriteLine(kp.Key + "\t" + kp.Value);
            }
            Console.ReadLine();
        }
    }
 
    public class Employee
    {
        public int empId { getset; }
        public string empName { getset; }
    }
}

In above code sample, we have created a class employee with fields empId and empName. We are storing several Employee objects in a HashTable with Key as empId and value as empName.

We have declared HashTable object hashTblEmployee and added Employee objects into it using add method which requires 2 parameters, one is Key object and second one is Value Object.

We are now retrieving each HashTable Key and Value using foreach loop. While retrieving elements from HashTable, it returns DictionaryEntry object which contains Key and Value properties corresponds to Key and Value of each hashTable element. Using this DictionaryEntry object we get Key and its corresponding Value from HashTable.

Below is a sample code for HashTable with basic and important functions.

+
Code
using System;
using System.Collections;
 
namespace HashTable_Collection
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp1 = new Employee() { empId = 111, empName = "Sam" };
            Employee emp2 = new Employee() { empId = 112, empName = "Jane" };
            Employee emp3 = new Employee() { empId = 113, empName = "Sachin" };
            Employee emp4 = new Employee() { empId = 114, empName = "Jack" };
 
            Hashtable hashTblEmployee = new Hashtable();
 
            //Add function
            hashTblEmployee.Add(emp1.empId, emp1.empName);
            hashTblEmployee.Add(emp2.empId, emp2.empName);
            hashTblEmployee.Add(emp3.empId, emp3.empName);
            hashTblEmployee.Add(emp4.empId, emp4.empName);
 
            foreach (DictionaryEntry kp in hashTblEmployee)
            {
                Console.WriteLine(kp.Key + "\t" + kp.Value);
            }
 
            Console.WriteLine("\n");
 
            //Count function
            Console.WriteLine("Number of elements in HashTable = {0}", hashTblEmployee.Count);
            Console.WriteLine("\n");
 
            //Contains/ContainsKey function
            Employee emp5 = new Employee() { empId = 111, empName = "Jim" };
 
            if (!hashTblEmployee.Contains(emp5.empId))
            {
                hashTblEmployee.Add(emp5.empId, emp5.empName);
                foreach (DictionaryEntry kp in hashTblEmployee)
                {
                    Console.WriteLine(kp.Key + "\t" + kp.Value);
                }
            }
            else
            {
                Console.WriteLine("Element with Key {0} already exists", emp5.empId);
            }
 
            //ContainsValue function
            if (hashTblEmployee.ContainsValue("Emma"))
            {
                Console.WriteLine("Emma is present in HashTable");
            }
            else
            {
                Console.WriteLine("Emmma is not present in HashTable");
            }
 
            //Remove function
            hashTblEmployee.Remove(111);
 
            Console.WriteLine("\nRemoving Element with key 111");
            foreach (DictionaryEntry kp in hashTblEmployee)
            {
                Console.WriteLine(kp.Key + "\t" + kp.Value);
            }
 
            Console.ReadLine();
        }
    }
 
    public class Employee
    {
        public int empId { getset; }
        public string empName { getset; }
    }
}

Add : Adds the element into HashTable. It requires 2 parameters, Key object and Value object.
Count: Returns the number of Elements.
Contains/ContainsKey : Determine whether specified Key is present in HashTable or not. Return true if present otherwise false. Contains function internally calls ContainsKey function. So you can use either Contains function or ContainsKey function, both will give you same result.
ContainsValue : Determine whether specified Value is present in HashTable or not. Return true if present otherwise false.
Remove: Remove specified Item from HashTable. Expect Key object to remove as parameter.

Below is the output of above program.

HashTable in C#

No comments :