Thursday 12 May 2016

Hash Table

HashTable

In this article I will explain about HashTable of C#.

Its collection type of object, which store on the basis of KEY , VALUE.
Key is use to access the element and its value.

HashTable required pair of Key,Value.

If you know the ASP.NET, recollect or check VIEWSTATE declaration and recall mechanism.

HashTable declration and recall is similar to VIEWSTATE, SESSION.



System.Collections namespace required to use HASHTABLE.

using System.Collections;




HashTable: Represents a collection of Key/Value pairs that are organized based on the has code of the key.

You can assign the value to HASHTABLE with two different ways.

  1. Method 1 to Assign Key/Value to HashTable
Hashtable _htable = new Hashtable();
_htable["WindowsPath"] = "C:\\Windows";

  1. Method 2 To Assign Key/Value to HashTable
      Hashtable _htable = new Hashtable();
_htable.Add("WindowsPath", "C:\\Windows");





Step 1
Create Console Application
Named: HashTableConsoleApp


HashTable having following method:
  1. Add : Add new item with Key and Value pair.
  1. Clear : Remove all items from HashTable.
  2. Clone :
  3. ContainsKey : Check given value exists in KEY side or not.

  1. ContainsValue : Check given value exists in VALUE side or not.


  1. CopyTo : Copy hashtable into array.



  1. Keys : Hashtable store value against title / heading of Keys.


  1. Remove :
  2. Values : Store the values of Keys.

  1. 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();
//Style 1 to add key/value
_htable["1"] = "Ashish";
//Style 2 to add key/value
_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");
//To Get total numbers of item in hashtable.
int TotalItems = _htable.Count;

Console.WriteLine("=================================");
Console.WriteLine("Displaying Total Numbers of Keys");
Console.WriteLine("=================================");

// Display the keys.
foreach (string key in _htable.Keys)
{
Console.WriteLine(key);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("==================================");
Console.WriteLine("Displaying Total Numbers of Values");
Console.WriteLine("==================================");

// Display the values.
foreach (string value in _htable.Values)
{
Console.WriteLine(value);
}

Console.WriteLine("Total Item in HashTable :" + Convert.ToString(TotalItems));

Console.WriteLine();
Console.WriteLine();
///Removing 6 Number of Keys
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("=================================");
// Display the keys.
foreach (string key in _htable.Keys)
{
Console.WriteLine(key);
}
Console.WriteLine();
Console.WriteLine();
Console.Read();

//To clear the hashtable.
_htable.Clear();


}
}
}


Happy Coding...


No comments:

Post a Comment

Please, Write your valuable comments..