Multiple ways to Iterate Dictionary in C# Examples

In C#, a dictionary is a data structure that stores key-value pairs. This tutorial provides multiple examples of iterating through a dictionary object in C#.

C# Iterate dictionary examples

There are multiple ways to iterate key and value pairs

  • using forEach.

    Iterate keys and values using foreach.

    Each iteration holds an object and an object contains a key and value pair

    foreach(var item in employees) {
          Console.WriteLine(item.Key + ": " + item.Value);
    
        }
    

    You can also use typed object KeyValuePair<> generics to hold the key and value pair of a given type

    //Iterate each object with KeyValuePair
    foreach(KeyValuePair<int, string> item in employees){
    
            Console.WriteLine(item.Key+": "+item.Value);
    }
    

    Another way is to use destructing syntax, which works in .Net 4.7 version onwards.

    foreach (var (key, value) in employees) {
            Console.WriteLine(key+": "+value);
    
    }
    

    Here is a complete example

    using System;
    using System.Collections.Generic;
    
    public class Program {
      public static void Main() {
        Dictionary < int, string > employees = new Dictionary < int, string > ();
        employees.Add(1, "one");
        employees.Add(2, "four");
        employees.Add(3, "two");
        employees.Add(4, "three");
        // Iterate each object
        foreach(var item in employees) {
          Console.WriteLine(item.Key + ": " + item.Value);
    
        }
        //Iterate each object with KeyValuePair
        foreach(KeyValuePair<int, string> item in employees)
        {
    
            Console.WriteLine(item.Key+": "+item.Value);
        }
    
      }
    }
    

    use Dictionary.Values to iterate only values use Dictionary.Keys to iterate only keys.

    Here is an example

    // Iterate only Values
    foreach(var item in employees.Values) {
          Console.WriteLine(item);
    
    }
    // Iterate only Keys
    foreach(var item in employees.Keys) {
          Console.WriteLine(item);
    
    }
    
  • Using LINQ

    In LINQ, one can convert a dictionary object into a list of KeyValuePairs using the ToList() method. This method returns a list of KeyValuePairs, allowing for easier iteration over each object.

    foreach (var item in dictionary.ToList())
    {
            Console.WriteLine(item.Key+": "+item.Value);
    }
    

    This approach has several advantages

    • It is useful for modifying data while iterating.
    • Since the dictionary is converted to a list, the list preserves the insertion order.
  • Parallel.ForEach function This is another of iterating dictionary concurrently using parallel technique in multiple threads.

    It iterates each item using parallel.ForEach function. item is of type KeyValuePair type

    Parallel.ForEach(dictionary, item =>
    {
              Console.WriteLine(item.Key+": "+item.Value);
    });
    

Conclusion

You learned multiple ways to iterate Dictionary in C#