DictionarymyDict = new Dictionary (); // creates a new dictionary with string keys and int values myDict.Add("one", 1); myDict.Add("two", 2); myDict.Add("three", 3); foreach (KeyValuePair kvp in myDict) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); }
Hashtable myHashtable = new Hashtable(); // creates a new hashtable myHashtable.Add("key1", "value1"); myHashtable.Add("key2", "value2"); myHashtable.Add("key3", "value3"); foreach (DictionaryEntry de in myHashtable) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); }This code creates a new hashtable and adds key-value pairs to it. It then uses a foreach loop to iterate through the hashtable and print each key-value pair to the console. The Compiler MapType is part of the C# standard library, which is included in the .NET Framework.