What is a Dictionary?
A dictionary is a general-purpose collection used to hold key-value pairs. The system defines a dictionary. The namespace System.Collection.Generics. Because of its dynamic nature, the dictionary's content expands to meet demand.
As an illustration
Dictionary<int, string> weekDays = new Dictionary<int, string>();
weekDays.Add(1, "Sunday");
weekDays.Add(2, "Monday");
weekDays.Add(3, "Tuesday");
weekDays.Add(4, "Wednesday");
weekDays.Add(5, "Thrusday");
weekDays.Add(6, "Friday");
weekDays.Add(7, "Saturday");
foreach (var day in weekDays)
{
Console.WriteLine("{0} - {1}", day.Key, day.Value);
}
What is a Hashtable?
A set of key/value pairs organized according to the key's hash code is called a hashtable. Put another way, a collection that uses a hash table for storage is created using a hashtable. The collection type that is defined in the System is the non-generic type. namespace for the system. collections. As long as they are utilized as keys in a hashtable, key objects must be immutable.
As an illustration
Hashtable weekDays = new Hashtable();
weekDays.Add(1, "Sunday");
weekDays.Add(2, "Monday");
weekDays.Add(3, "Tuesday");
weekDays.Add(4, "Wednesday");
weekDays.Add(5, "Thrusday");
weekDays.Add(6, "Friday");
weekDays.Add(7, "Saturday");
foreach (DictionaryEntry day in weekDays)
{
Console.WriteLine("{0} - {1}", day.Key, day.Value);
}
Hashtable vs Dictionary
Hashtable |
Dictionary |
A non-generic collection is called a hashtable. |
A dictionary is an all-purpose compilation. |
Under the System. Collections namespace, a hashtable, is defined. |
Under the System.Collections.Generic namespace, dictionary is defined. |
The recorded values' order is not preserved. |
It consistently preserves the recorded values' order. |
It is not necessary to declare the type of key or value in a hashtable. |
You have to indicate the kind of key and value in the Dictionary. |
Because of packaging and unpacking, the data retrieval is slower than the Dictionary. |
The lack of boxing and unpacking makes the data retrieval faster than using a hashtable. |
If a key is attempted to be accessed in a hashtable that does not exist, null values will be returned. |
When accessing a key in a dictionary that is not included in the specified dictionary, an error will be displayed. |
It doesn't risk threading. |
Only public static members can access it, but it is likewise thread-safe. |
[MemoryDiagnoser]
public class BenchmarkProcess
{
[Benchmark]
public void BenchmarkDictionary()
{
CreateBenchmarkDictionary();
}
[Benchmark]
public void BenchmarkHashtable()
{
CreateBenchmarkHashTable();
}
const int ONE_HUNDRED_THOUSAND = 100000;
public Dictionary<int, string> CreateBenchmarkDictionary()
{
var dictionary = new Dictionary<int, string>();
for (var i = 0; i < ONE_HUNDRED_THOUSAND; i++)
{
dictionary.Add(i, $"Dictionary element {i}");
}
return dictionary;
}
public Hashtable CreateBenchmarkHashTable()
{
var hashTable = new Hashtable();
for (var i = 0; i < ONE_HUNDRED_THOUSAND; i++)
{
hashTable.Add(i, $"Hashtable element {i}");
}
return hashTable;
}
}
The BenchmarkDotNet.The artifacts folder would contain the benchmark results once the project had been executed in Release mode.
The BenchmarkHashTable method takes about 32.44 ms for 100,000 entries, while the BenchmarkDictionary method takes about 21.59 ms. Additionally, the dictionary method uses less RAM.
Let us conduct another test with 10,000,000 elements this time.
Hashtable is outperformed by Dictionary even with a comparatively larger input. Hashtable takes 3.992 seconds on average, while Dictionary takes about 2.864 seconds on average.
Approximately 1.35 GB of memory is used by the Dictionary technique, while 1.96 GB is used by the Hashtable method.
Summary
This article has taught us about Dictionary and Hashtable, two popular C# collections, and has also shown us how they differ from one another. Additionally, we now know that there are situations in which a dictionary is better than a hashtable.
We learned the new technique and evolved together.
Happy coding!