Diff b/w data dictionary and hash table? [Resolved]

Posted by Hariinakoti under C# on 6/25/2012 | Points: 10 | Views : 14282 | Status : [Member] | Replies : 3
Hi Friends,
What is the Diff b/w data dictionary and hash table?

Thanks & Regards
Hari



Responses

Posted by: Santhiram on: 6/25/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Basically Collections & Generics are useful in handling group of Objects.In .net,all the collections objects comes under the interface IEnumerable Which inturn has ArrayList(Index -Value)) & HashTable(Key- Value).After .net framework 2.0,ArrayList & HashTable were replaced with List & Dictionary .Now the Arraylist & HashTable are no more used in now a days projects.

Coming to difference between HashTable & Dictionary,Dictionary is generic whereas Hastable is not Generic.We can add any type of object to HashTable ,but while reteriving we need to Cast it to the required Type.So it is not type safe.But to dictionary,while declaring itself we can specify the type of Key & Value ,so no need to cast while retreiving.Let me explain it with an Example.

HashTable Program:
class HashTableProgram
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1,"One");
ht.Add(2,"Two");
ht.Add(3,"Three");
foreach (DictionaryEntry de in ht)
{
int Key = (int)de.Key; //Casting
string value = de.Value.ToString(); //Casting
Console.WriteLine( Key + " " + value);
}

}

Dictionary Example
class DictionaryProgram
{
static void Main(string[] args)
{
Dictionary<int,string> dt = new Dictionary<int,string>();
dt.Add(1,"One");
dt.Add (2,"Two");
dt.Add (3,"Three");
foreach (KeyValuePair<int,String> kv in dt)
{
Console.WriteLine( kv.Key + " " + kv.Value);
}
}
}


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

Posted by: Rajkatie on: 6/25/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Kindly refer following link
http://stackoverflow.com/questions/876656/difference-between-dictionary-and-hashtable

Rajesh Patel
.Net Trainer(MSCD | MCT | MS)
Brainbench (Asp.Net 4.5 and C# 5.0)

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

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

Login to post response