How To Use Hashtable class In C# Language

Introduction

Hash table is a part of collection in  c sharp, which store the key and value. It means hash table class is represent a pair of key and value that organized based on the hash code of the key. It is useful to access elements using by key.

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

namespace how_to_use_hashtable_class_in_c_sharp

{

    class Hash_table

    {

        static void Main(string[] args)

        {

            Hashtable htable = new Hashtable(); //create instance of hash table

 

 

            htable.Add("001", "Arvind kumar");  //add value in hash table in key and value

            htable.Add("002", "Nitin Bhardwaj");

            htable.Add("003", "Manish Kumar");

           

 

            if (htable.ContainsValue("Rajkumar"))  // check conduction

            {

                Console.WriteLine("This student name is already in the list");

            }

            else

            {

                htable.Add("004", "Rajkumar");   // value add

            }

 

            ICollection key = htable.Keys;    // Get a collection of the keys.

 

            foreach (string n in key)

            {

                Console.WriteLine(n + ": " + htable[n]); // show value of hash table

            }

            Console.ReadKey();

        }

    }

}


Output:


output.jpg


Ebook Download
View all
Learn
View all