Avatar ·

Why is Dictionary<TKey, TValue> better than Hashtable in C#?

Hi everyone! I recently started learning C# and came across collections. I see that there is `Hashtable` (from `System.Collections`) and `Dictionary` (from `System.Collections.Generic`). They both seem to store key-value pairs, but everywhere they recommend using `Dictionary`. I wrote a small example with `Hashtable`: ```csharp Hashtable table = new Hashtable(); table.Add("key1", 123); table.Add("key2", "string"); foreach (DictionaryEntry entry in table) { Console.WriteLine($"{entry.Key}: {entry.Value}"); } ``` And with `Dictionary`: ```csharp Dictionary dict = new Dictionary(); dict.Add("key1", 123); dict.Add("key2", 456); // everything is strict here foreach (KeyValuePair kvp in dict) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); }