Dictionary initializer C#
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | class Program { static void Main(string[] args) { //initialize a new empty dictionary. Dictionary<int, string> plants = new Dictionary<int, string>(); //add keyvaluepair to dictionary. plants.Add(1, "Bermuda Cress"); plants.Add(2, "Bulbous Cress"); plants.Add(3, "Pale Corydalis"); //initialize a dictionary with values. Dictionary<int, string> birds = new Dictionary<int, string>() { {1,"Roseate Tern"}, {2,"Antarctic Tern"}, {3,"Black Skimmer"}, {4,"Atlantic Puffin"} }; Console.WriteLine( "plants dictionary keys and values.........."); foreach (KeyValuePair<int, string> pair in plants) { Console.WriteLine(pair.Key + " ........ " + pair.Value); } Console.WriteLine("birds dictionary keys and values.........."); foreach (KeyValuePair<int, string> pair in birds) { Console.WriteLine(pair.Key + " ........ " + pair.Value); } Console.ReadLine(); } } |
Output: