Dictionary to list
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 | class Program { static void Main(string[] args) { //create a new dictionary object. Dictionary<int, string> dictionary = new Dictionary<int, string>(); dictionary.Add(1, "Red Birch"); dictionary.Add(2, "River Birch"); dictionary.Add(3, "Silver Birch"); dictionary.Add(4, "Spice Birch"); Console.WriteLine("dictionary keys and values.........."); foreach (KeyValuePair<int, string> p in dictionary) { Console.WriteLine(p.Key + " ------- " + p.Value); } //convert dictionary to generic list List<KeyValuePair<int, string>> plants = dictionary.ToList(); Console.WriteLine("\ngeneric list elements.........."); foreach (KeyValuePair<int, string> pair in plants) { Console.WriteLine(pair.Key + " ------- " + pair.Value); } Console.ReadLine(); } } |
Output: