It's a collection type of object, which stores on the basis of KEY , VALUE.
Key is used to access the element and its value.
HashTable requires a pair of Key,Value.
If you know the ASP.NET, recollect or check VIEWSTATE declaration and recall mechanism.
Hash Table declaration and recall is similar to VIEWSTATE, SESSION.
System.Collections namespace is required to use HASHTABLE.
- using System.Collections;
Hash Table
Represents a collection of Key/Value pairs that are organized based on the hash code of the key.
You can assign the value to HASHTABLE with two different ways.
- Method 1 to Assign Key/Value to HashTable.
- Hashtable _htable = new Hashtable();
- _htable["WindowsPath"] = "C:\\Windows";
- Method 2 To Assign Key/Value to HashTable.
- Hashtable _htable = new Hashtable();
- _htable.Add("WindowsPath", "C:\\Windows");
Step 1: Create Console Application.
HashTable has the following method:
- Add: Add new item with Key and Value pair.
- Clear: Remove all items from HashTable.
- Clone:
- ContainsKey: Check if the given value exists in KEY side or not.
- ContainsValue: Check if the given value exists in VALUE side or not.
- CopyTo: Copy hashtable into array.
- Keys: Hashtable stores value against title / heading of Keys.
- Remove:
- Values: Store the values of Keys.
- Count: Total numbers of keys/values pair in hashtable.
Code
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HashTableConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- Hashtable _htable = new Hashtable();
-
- _htable["1"] = "Ashish";
-
- _htable.Add("2", "Suhana");
- _htable.Add("3", "Aakash");
- _htable.Add("4", "Simran");
- _htable.Add("5", "Rajesh");
- _htable.Add("6", "Mahesh");
- _htable.Add("7", "Suresh");
- _htable.Add("8", "Pritesh");
- _htable.Add("9", "Jayesh");
- _htable.Add("10", "Mayuresh");
- _htable.Add("11", "Ramesh");
- _htable.Add("12", "Rajniesh");
-
- int TotalItems = _htable.Count;
- Console.WriteLine("=================================");
- Console.WriteLine("Displaying Total Numbers of Keys");
- Console.WriteLine("=================================");
-
- foreach(string key in _htable.Keys)
- {
- Console.WriteLine(key);
- }
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("==================================");
- Console.WriteLine("Displaying Total Numbers of Values");
- Console.WriteLine("==================================");
-
- foreach(string value in _htable.Values)
- {
- Console.WriteLine(value);
- }
- Console.WriteLine("Total Item in HashTable :" + Convert.ToString(TotalItems));
- Console.WriteLine();
- Console.WriteLine();
-
- Console.WriteLine("==================================");
- Console.WriteLine("Removing 6th Item of HashTable");
- Console.WriteLine("==================================");
- _htable.Remove("6");
- Console.WriteLine("=================================");
- Console.WriteLine("Check 6th Number Keys Removed");
- Console.WriteLine("=================================");
-
- foreach(string key in _htable.Keys)
- {
- Console.WriteLine(key);
- }
- Console.WriteLine();
- Console.WriteLine();
- Console.Read();
-
- _htable.Clear();
- }
- }
- }