Dictionary:
·
It returns error if we try to find a key which does not exist.
·
It is faster than a Hashtable because there is no boxing and
unboxing.
·
Only public static members are thread safe.
·
Dictionary is a generic type which means we can use it with
any data type.
·
static typing (and
compile-time verification)
Hashtable:
·
It returns null if we try to find a key which does not exist.
·
It is slower than dictionary because it requires boxing and
unboxing.
·
All the members in a Hashtable are thread safe,
·
Hashtable is not a generic type,
DICTIONARY VS HASHTABLE in C# REAL TIME EXAMPLES
Differences Between Hashtable and Dictionary
1. Hashtable is threadsafe and while Dictionary is not.
// Creates a synchronized wrapper around the Hashtable.
Ex:Hashtable myhashtable = Hashtable.Synchronized(hash);
The Synchronized method is thread safe for multiple readers and writers. Furthermore, the synchronized wrapper ensures that there is only one writer writing at a time.
2. Dictionary is types means that the values need not to boxing while Hashtable
values need to be boxed or unboxed because it stored the values and keys as
objects.
3. When you try to get the value of key which does not exists in the collection, the
dictionary throws an exception of 'KeyNotFoundException' while hashtable returns
null value.
4. When using large collection of key value pairs hashtable would be considered more
efficient than dictionary.
5. When we retrieve the record in collection the hashtable does not maintain the order
of entries while dictionary maintains the order of entries by which entries were added.
6. Dictionary relies on chaining whereas Hashtable relies on rehashing.
7.Dictionary is a strongly typed generic collection while hashtable collection takes a object datatype
Example
public void MethodHashTable()
{
Hashtable objHashTable = new Hashtable();
objHashTable.Add(1, 100); // int
objHashTable.Add(2.99, 200); // float
objHashTable.Add('A', 300); // char
objHashTable.Add("4", 400); // string
lblDisplay1.Text = objHashTable[1].ToString();
lblDisplay2.Text = objHashTable[2.99].ToString();
lblDisplay3.Text = objHashTable['A'].ToString();
lblDisplay4.Text = objHashTable["4"].ToString();
// ----------- Not Possible for HashTable ----------
//foreach (KeyValuePair<string, int> pair in objHashTable)
//{
// lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
//}
}
Dictionary Example
public void MethodDictionary()
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
//dictionary.Add(1, -2); // Compilation Error
foreach (KeyValuePair<string, int> pair in dictionary)
{
lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
}
}
public void MethodHashTable()
{
Hashtable objHashTable = new Hashtable();
objHashTable.Add(1, 100); // int
objHashTable.Add(2.99, 200); // float
objHashTable.Add('A', 300); // char
objHashTable.Add("4", 400); // string
lblDisplay1.Text = objHashTable[1].ToString();
lblDisplay2.Text = objHashTable[2.99].ToString();
lblDisplay3.Text = objHashTable['A'].ToString();
lblDisplay4.Text = objHashTable["4"].ToString();
// ----------- Not Possible for HashTable ----------
//foreach (KeyValuePair<string, int> pair in objHashTable)
//{
// lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
//}
}
Dictionary Example
public void MethodDictionary()
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("cat", 2);
dictionary.Add("dog", 1);
dictionary.Add("llama", 0);
dictionary.Add("iguana", -1);
//dictionary.Add(1, -2); // Compilation Error
foreach (KeyValuePair<string, int> pair in dictionary)
{
lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
}
}