Dictionary get range
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 | class Program { static void Main(string[] args) { //initialize a dictionary with keys and values. Dictionary<int, string> birds = new Dictionary<int, string>() { {1,"Hyacinth Macaw"}, {2,"Burrowing Parakeet"}, {3,"Vulturine Parrot"}, {4,"Great Blue Turaco"}, {5,"Common Cuckoo"}, {6,"Brush Cuckoo"} }; Console.WriteLine("dictionary keys and values.........."); foreach (KeyValuePair<int, string> pair in birds) { Console.WriteLine(pair.Key + " ........ " + pair.Value); } int rangeStart = 2; int rangeEnd = 4; Console.WriteLine("\nget dictionary keys and values at index range 2 to 4"); for (int i = rangeStart; i <= rangeEnd; i++) { Console.WriteLine("key: " + birds.Keys.ElementAt(i)+" value: " + birds.Values.ElementAt(i)); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | dictionary keys and values.......... 1 ........ Hyacinth Macaw 2 ........ Burrowing Parakeet 3 ........ Vulturine Parrot 4 ........ Great Blue Turaco 5 ........ Common Cuckoo 6 ........ Brush Cuckoo get dictionary keys and values at index range 2 to 4 key: 3 value: Vulturine Parrot key: 4 value: Great Blue Turaco key: 5 value: Common Cuckoo |
Very useful program. I found this website useful and informative specially for computer science students. I really appreciate to author.