How to use KeyValuePair
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Program { static void Main(string[] args) { Dictionary<string, ConsoleColor> colors = new Dictionary<string, ConsoleColor>(); colors.Add("Blue", ConsoleColor.Blue); colors.Add("Green", ConsoleColor.Green); colors.Add("Magenta", ConsoleColor.Magenta); colors.Add("White", ConsoleColor.White); colors.Add("Yellow", ConsoleColor.Yellow); Console.WriteLine( "Dictionary Keys and Values.....\n"); foreach (KeyValuePair<string, ConsoleColor> pair in colors) { Console.ForegroundColor = pair.Value; Console.WriteLine("Key:"+pair.Key + "=> Value:" + pair.Value); } Console.ReadLine(); } } |
Output: