Get key value pair from dictionary
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 |
class Program { static void Main(string[] args) { //initialize a dictionary with keys and values. Dictionary<int, string> birds = new Dictionary<int, string>() { {10,"Parasitic Skua"}, {20,"Great Skua"}, {30,"Atlantic Puffin"}, {40,"Little Auk"}, {50,"Guillemot"} }; Console.WriteLine("dictionary keys and values.........."); foreach (KeyValuePair<int, string> pair in birds) { Console.WriteLine(pair.Key + " ........ " + pair.Value); } //get keyvaluepair from dictionary at index 1. KeyValuePair<int, string> pairOfIndex1 = birds.ElementAt(1); //get keyvaluepair from dictionary at index 3. KeyValuePair<int, string> pairOfIndex3 = birds.ElementAtOrDefault(3); Console.WriteLine("\n\ndictionary KeyValuePair at index 1......"); Console.WriteLine(pairOfIndex1.Key + " ........ " + pairOfIndex1.Value); Console.WriteLine("\n\ndictionary KeyValuePair at index 3......"); Console.WriteLine(pairOfIndex3.Key + " ........ " + pairOfIndex3.Value); Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
dictionary keys and values.......... 10 ........ Parasitic Skua 20 ........ Great Skua 30 ........ Atlantic Puffin 40 ........ Little Auk 50 ........ Guillemot dictionary KeyValuePair at index 1...... 20 ........ Great Skua dictionary KeyValuePair at index 3...... 40 ........ Little Auk |