Mar 11, 2020

HASHTABLE COLLECTION IN C#


c# hashtable example, c# hashtable dictionary, c# hashtable get key by value,new hashtable, c# hashtable dictionary
c# hashtable

C# HASHTABLE

            "Hashtable is a type of collection just like an ArrayList that store objects type data in memories with object key and value", but mostly the programmer prefer Hashtable against array list why because Hashtable is little bit different than ArrayList in C# see post's bottom.  
SYNTAX OF HASHTABLE
Like ArrayList it's require to add the System. Collections library as directive. And then
Hashtable obj = new Hashtable(); // in place of obj the name of Hashtable will write
SYNTAX OF ADDING & REMOVING VALUES IN HASHTABLE 
             For add values in Hashtable we need to use Add keyword as we did in ArrayList, but here we have to name the keys explicitly and then assign values to it.
obj. Add("object Key", object value);
Like this obj. Add("Stdid", 5809);
           For remove just specify the object key
obj. Remove("object key");
Like this obj. Remove("stdid");

EXAMPLE OF HASHTABLE COLLECTION IN C#

       Suppose a student data base
       static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add("stdid", 5809);
            ht.Add("name", "Aslam");
            ht.Add("surname", "Maaz");
            ht.Add("Fname", "Qadir");
            ht.Add("Depname", "Computer Science");
            ht.Add("shift", "morning");
            ht.Add("fee", 20129.45);
            ht.Remove("stdsurname");
            foreach (object key in ht.Keys)
                Console.WriteLine(key+  " ; "  +ht[key]);
            Console.ReadKey();
        }

METHOD OF SORTING OF DATA IN HASHTABLE /Hashcode

When we execute and after watch outputs we realize that the data is not in sequence as we provided in program this is why? See it is just because of Hashcode assign every object key in Hashtable the data store according to the Hashcode so that whys it's disturbed our provided sequence. The data comes according to the Hashcode sequence.
You can check the Hashcode by using this syntax
Console.WriteLine("fee".GetHashCode());
Let try to see the Hashcode of above program Hashtable keys
static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();
            ht.Add("stdid", 5809);
            ht.Add("name", "Aslam");
            ht.Add("surname", "Maaz");
            ht.Add("Fname", "Qadir");
            ht.Add("Depname", "Computer Science");
            ht.Add("shift", "morning");
            ht.Add("fee", 20129.45);
            ht.Remove("stdsurname");
            Console.WriteLine("fee".GetHashCode());
            foreach (object key in ht.Keys)
                Console.WriteLine(key+ "  ;  " +key.GetHashCode());
            Console.ReadKey();
        }

Hashtable VS ArrayList in c#

            Since the both term is type of collection and store data but a little bit difference between these are the method of storing data, in ArrayList data store in sequence of index like 0,1,2,3 … against every key' that is default index numbers' a value is store and if we want to call the value in ArrayList it is mandatory to remember the key's number against that the value store, but Hashtable is independent form this problem the key in Hashtable is user defined, key can be any data type  we can set a numeric value or string means the data store in key is object type and then assign the value to the keys, later which is easy to fetch and call by its name not need to remember any number to call the values in Hashtable see it In example.


0 comments:

Post a Comment

Please do not enter any spam link in the comment box