C# | Difference between Hashtable and Dictionary
In C#, Hashtables and Dictionaries are two commonly used collection type for storing and retrieving key-value pairs. The following example demonstrates creating a Hashtable and adding elements. Example: Create and Add Elements Hashtable numberNames = new Hashtable (); numberNames.Add(1, "One" ); //adding a key/value using the Add() method numberNames.Add(2, "Two" ); numberNames.Add(3, "Three" ); //The following throws run-time exception: key already added. //numberNames.Add(3, "Three"); foreach ( DictionaryEntry de in numberNames) Console .WriteLine( "Key: {0}, Value: {1}" , de.Key, de.Value); Try it A Dictionary can be created by passing the type of keys and values it can store. Example: Create Dictionary and Add Elements IDictionary < int , string > numberNames = new Dictionary < int , string >(); numberNames.Add(1, "One" ); //adding a key/value using the Add() method numberNames.Add(2, "...