Data Dictionary
and Hash Table both are used to collect data as Key/Value pair. We can
differentiate both based on following criteria
1.
Generic
2.
Performance
3.
Thread
Generic:
|
Dictionary is a Generic Type but Hash Table is not Generic Type
Note: Generic
Type is a type which is used to hold the value and Reference types. As we know there are two fundamental categories of types.
1. value types
(example: structures, primitive type, enums)
2. Reference types
(example: Classes, interface, arrays, strings etc..,)
Performance
|
Data Dictionary is faster because there is no boxing/unboxing. And Hash table is
slow because - Hash table uses Object to hold things .So you have to take care of type safety and
cast it to appropriate data type when you take it out.
Hash
Table have to box and unbox object which may consume memory that will affect
performance.
Thread
Safety
|
Dictionary
is Type safety because we can’t insert random object into it. And we need not to
cast value
Example: Hash Table
Hashtable objHashTable = new Hashtable();
objHashTable.Add(200, 100); // int
objHashTable.Add(8.3 9, 200); // float
objHashTable.Add('C', 300); // char
objHashTable.Add("7", 400); // string
mylblDisplay1.Text = objHashTable[200].ToString();
mylblDisplay2.Text = objHashTable[8.3 9].ToString();
mylblDisplay3.Text = objHashTable['C'].ToString();
mylblDisplay4.Text = objHashTable["7"].ToString();
Example: DataDictionary
Dictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0); dictionary.Add("iguana", -1);
//
----------- Not Possible for HashTable ----------
//dictionary.Add(1, -2); // Compilation Error
foreach (KeyValuePair<string, int> pair in dictionary) { lblDisplay.Text = pair.Value + " " + lblDisplay.Text; }
|
Important
Notes:
·
We cannot use dictionary
(generics) with web services. The reason is no web service
Thanks to publish such type of blogs.
ReplyDeleteYour blogs are help full to us in interview.
Thanks for such blogs,it was helpfull
ReplyDelete