Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to use C# HashTable Class



Hashtable in C# represents a collection of key/value pairs which maps keys to value. Any non-null object can be used as a key but a value can. We can retrieve items from hashTable to provide the key. Both keys and values are Objects.

The commonly used functions in Hashtable are :

  • Add: To add a pair of Value in HashTable
  • ContainsKey: Check if a specified key exists or not
  • ContainsValue: Check the specified Value exist in HashTable
  • Remove: Remove the specified Key and corresponding Value

Add: To add a pair of Value in HashTable

Syntax

HashTable.Add(Key,Value)
  • Key: The Key Value
  • Value: The Value of the corresponding key
Hashtable ht;
ht.Add("1", "Sunday");

ContainsKey : Check if a specified key exist or not

Syntax

bool HashTable.ContainsKey(key)
  • Key: The Key-Value for search in HahTable
  • Returns: return true if the item exists else false
ht.Contains("1");

ContainsValue: Check the specified Value exist in HashTable

Syntax

bool HashTable.ContainsValue(Value)
  1. Value: Search the specified Value in HashTable
  2. Returns: return true if the item exists else false
ht.ContainsValue("Sunday")

Remove: Remove the specified Key and corresponding Value

Syntax

HashTable.Remove(Key)
  1. Key: The key of the element to remove
ht.Remove("1");

The following source code shows some crucial operations in a HashTable

using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Hashtable weeks = new Hashtable();
            weeks.Add("1", "SunDay");
            weeks.Add("2", "MonDay");
            weeks.Add("3", "TueDay");
            weeks.Add("4", "WedDay");
            weeks.Add("5", "ThuDay");
            weeks.Add("6", "FriDay");
            weeks.Add("7", "SatDay");
            //Display a single Item 

            MessageBox.Show(weeks["5"].ToString ());
            //Search an Item 
            if (weeks.ContainsValue("TueDay"))
            {
                MessageBox.Show("Find");
            }
            else
            {
                MessageBox.Show("Not find");
            }
            //remove an Item 
            weeks.Remove("3");
            //Display all key value pairs
            foreach (DictionaryEntry day in weeks )
            {
                MessageBox.Show (day.Key + "   -   " + day.Value );
            }
        }
    }
}

When you execute this C# program, it will add seven entries in the Hashtable. The first message it will display the item 5. Then it checks the value “Tuesday” is existing or not. Next, it removes the third item from HashTable. Finally, it displays all items exist in the HashTable.


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More