Iterate over a dictionary in C#
This post will discuss how to iterate over a dictionary in C#.
1. Using foreach Loop
We can loop through all KeyValuePair<K,V> in the dictionary and print the corresponding Key and Value from each pair.
|
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 |
using System; using System.Collections.Generic; public class Example { public static void PrintDict<K, V>(Dictionary<K, V> dict) { foreach (KeyValuePair<K, V> entry in dict) { Console.WriteLine(entry.Key + " : " + entry.Value); } } public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; PrintDict(dict); } } /* Output: key1 : value1 key2 : value2 */ |
We can also iterate over the collection of keys and fetch the value using the Item[TKey] property.
|
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 |
using System; using System.Collections.Generic; public class Example { public static void PrintDict<K, V>(Dictionary<K, V> dict) { foreach (K key in dict.Keys) { Console.WriteLine(key + " : " + dict[key]); } } public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; PrintDict(dict); } } /* Output: key1 : value1 key2 : value2 */ |
Starting with C# 7.0, multiple fields can be retrieved from an object in a single deconstruct operation by assigning selected values to individual variables. The following example uses type inference when deconstructing inside the foreach loop, which lets us unpackage key and value in KeyValuePair<K,V> in a single operation.
|
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 |
using System; using System.Collections.Generic; public class Example { public static void PrintDict<K, V>(Dictionary<K, V> dict) { foreach (var (key, value) in dict) { Console.WriteLine(key + " : " + value); } } public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; PrintDict(dict); } } /* Output: key1 : value1 key2 : value2 */ |
We can also explicitly declare the type of each field inside the parentheses, as shown below:
|
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 |
using System; using System.Collections.Generic; public class Example { public static void PrintDict<K, V>(Dictionary<K, V> dict) { foreach ((K key, V value) in dict) { Console.WriteLine(key + " : " + value); } } public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; PrintDict(dict); } } /* Output: key1 : value1 key2 : value2 */ |
2. Using for loop
We can also use a for-loop to iterate over a directory. To get KeyValuePair<K,V> at a specified index in a sequence, use ElementAt() method.
|
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 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void PrintDict<K, V>(Dictionary<K, V> dict) { for (int i = 0; i < dict.Count; i++) { KeyValuePair<K, V> entry = dict.ElementAt(i); Console.WriteLine(entry.Key + " : " + entry.Value); } } public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; PrintDict(dict); } } /* Output: key1 : value1 key2 : value2 */ |
3. Using ParallelEnumerable.ForAll() method
A simple and fairly efficient solution to iterate over large dictionaries is using the ParallelEnumerable.ForAll() method. This method is demonstrated below:
|
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 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void PrintDict<K, V>(Dictionary<K, V> dict) { dict.AsParallel() .ForAll(entry => Console.WriteLine(entry.Key + " : " + entry.Value)); } public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; PrintDict(dict); } } /* Output: key1 : value1 key2 : value2 */ |
4. Using String.Join() method
Finally, the String.Join() method can be used to print all KeyValuePair<K,V> in dictionary separated by a delimiter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; Console.WriteLine(String.Join(Environment.NewLine, dict)); } } /* Output: [key1, value1] [key2, value2] */ |
That’s all about iterating over a Dictionary in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)