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.
- Method 1 to Assign Key/Value to HashTable
Hashtable
_htable = new
Hashtable();
_htable["WindowsPath"]
= "C:\\Windows";
- Method 2 To Assign Key/Value to HashTableHashtable _htable = new Hashtable();
_htable.Add("WindowsPath",
"C:\\Windows");
Step 1
Create Console Application
Named: HashTableConsoleApp
HashTable having following method:
- Add : Add new item with Key and Value pair.
- Clear : Remove all items from HashTable.
- Clone :
- ContainsKey : Check given value exists in KEY side or not.
- ContainsValue : Check given value exists in VALUE side or not.
- CopyTo : Copy hashtable into array.
- Keys : Hashtable store 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();
//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...
Email ID: mr.manojbkalla@gmail.com
WebSite : http://manojbkalla.weebly.com
No comments:
Post a Comment
Please, Write your valuable comments..