Dictionary foreach 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 |
class Program { static void Main(string[] args) { //create a new dictionary object. Dictionary<int, string> plants = new Dictionary<int, string>(); //add keyvaluepair to dictionary. plants.Add(1, "Canada Root"); plants.Add(2, "Skunk Cabbage"); plants.Add(3, "Cabinet Cherry"); plants.Add(4, "Brilliant Coneflower"); plants.Add(5, "Cutleaf Coneflower"); Console.WriteLine( "dictionary keys and values.........."); //using foreach loop to get all keyvaluepair of dictionary. foreach (KeyValuePair<int, string> pair in plants) { Console.WriteLine( pair.Key + " ........ " + pair.Value); } Console.ReadLine(); } } |
Output: