Avatar ·

Pourquoi Dictionary<TKey, TValue> est-il meilleur que Hashtable en C# ?

Bonjour à tous ! J'ai récemment commencé à apprendre C# et je suis tombé sur les collections. Je vois qu'il y a `Hashtable` (de `System.Collections`) et `Dictionary` (de `System.Collections.Generic`). Apparemment, les deux stockent des paires clé-valeur, mais partout on recommande d'utiliser `Dictionary`. J'ai écrit un petit exemple avec `Hashtable` : ```csharp Hashtable table = new Hashtable(); table.Add("key1", 123); table.Add("key2", "строка"); foreach (DictionaryEntry entry in table) { Console.WriteLine($"{entry.Key}: {entry.Value}"); } ``` Et avec `Dictionary` : ```csharp Dictionary dict = new Dictionary(); dict.Add("key1", 123); dict.Add("key2", 456); // ici tout est strict foreach (KeyValuePair kvp in dict) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); }