HashTable:-
A hash table is used when you need to access an
element using key. Each item in a hash table has a key/value pair. The key is
used to access the item in collection.
Properties
Of HashTable Class:-
Count :- Gets the number of elements actually contained
thr hashtable.
IsFixedSize :- Gets a value indicating whether the hashtable
has fixed size.
IsReadOnly :- Gets
a value indicating whether the hashtable has ReadOnly.
Item :- Gets or sets the elements at specified
index.
Key :- Gets an ICollection containing the keys in
the hashtable.
Value :- Gets an ICollection containing the values
in the hashtable.
Methods
Of HashTable Class :-
There are various methods in hashtable class for
ex. Add(), Clear(),Remove() etc. I have explained all methods in below example
of hashtable.
Example:-
using System;
using System.Collections;
using
System.Linq;
using
System.Text;
using
System.Data;
namespace
CollectionExample
{
class Program
{
//HashList
Example-------------------------------------------------------------------------
Console.WriteLine("-------------Hash table
Example---------------");
Hashtable
ht = new Hashtable();
ht.Add("1",
"Sushil");
ht.Add("2",
"Kumar");
ht.Add("3",
"Sanjay");
ht.Add("4",
"Muthu");
ht.Add("5",
"Ashu");
ht.Remove("1");
if
(ht.ContainsValue("Mantra"))
{
Console.WriteLine("This name already exist in HashTable.");
}
else
{
ht.Add("6",
"Mantra");
}
ICollection
key = ht.Keys;// to get collection of key
foreach
(string s in
key)
{
Console.WriteLine(s
+ " " + ht[s]);
}
Console.ReadKey();
}
}
}
Output:-
-------------Hash table Example---------------
3 Sanjay
6 Mantra
4 Muthu
5 Ashu
2 Kumar
No comments:
Post a Comment
Note: only a member of this blog may post a comment.