how to use HASHTABLE in .net with example

Posted by Sekar.C under C# on 4/17/2012 | Points: 10 | Views : 69045 | Status : [Member] | Replies : 14
Hi,
how to use HASHTABLE in .net with example

Regards
Sekar.c



Responses

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
Hashtable optimizes lookups. This type is used in the C# language. It computes a hash of each key you add. It then uses this hash code to look up the element very quickly. It is an older .NET Framework type. It is slower than the generic Dictionary type.



Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
For example, refer this link..

http://www.dotnetperls.com/hashtable

Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
its same as lists...

but hash table have keys for each values..

u can get exact values using keys...

Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
HashTable stores a key-value pair type collection of data.

Declaration:

Dim sampleHt as Hashtable = New Hashtable
sampleHt.add("Area",1000)
sampleHt.add("Perimeter",55)

NOTE:

Both key and value are object

We can retrieve the items from the hashtable by providing the key.

------------------------------------------------
Learn throughout life

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
the key values are UNIQUE

Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
Remove:

hash_table_name.Remove(key);

Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
Remove All Values:

hash_table_name.Clear()

Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
ContainsKey(Key)


its returns true or false..

true- that hash table contains the checking key value..

False - not contain..

Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sakthi.Singaravel on: 4/17/2012 [Member] Silver | Points: 25

Up
0
Down
ContainsValue(value):

true- contains the hash table as checking value
false - not contains

Regards,
Singaravel M

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sridevi on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
Using hashtable we can acheive data uniqueness in C#. Data uniqueness means without having null values and duplicate data.

EXAMPLE:


Hashtable h=new Hashtable();
h.add("a","one");
h.add("b","two");
h.add("c","three");

TO RETREIVE:
Dictionary entry is an interface used to retrieve the data present inside the hashtable.

EXAMPLE
foreach(DictionaryEntry d in h)
{
console.writeline(d.key);
console.writeline(d.value);
}

Regards,
Sridevi.

Sridevi

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vinay13mar on: 10/27/2012 [Member] Starter | Points: 25
Posted by: Vinay13mar on: 10/27/2012 [Member] Starter | Points: 25
Posted by: Nicolcrome on: 4/18/2013 [Member] Starter | Points: 25

Up
0
Down
follow the link , it shows all functions reagrding hashtable with full source code

http://csharp.net-informations.com/collection/csharp-hashtable.htm

nicol


Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhukiran345 on: 4/18/2013 [Member] Starter | Points: 25

Up
0
Down
In simple Hashtable is that which optimizes Searching criteria. That means a person having Surname.. In a group of people a person can easily been identified with his Surname.In the same manner Hash table contains Key,Value pairs which helps for optimizing Lookup's.
Lookup means: A procedure which a value is been searched until it is found.
Items in Hashtable are added with Add() method.

Example in c#::
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen";

foreach (DictionaryEntry entry in hashtable)
{
Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
}

Thanks,
Prabhu Kiran Bommareddy

Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response