How to iterate over a C# dictionary?

A Dictionary in C# is a collection of key-value pairs that can be iterated using several different approaches. Each method provides access to keys, values, or both simultaneously.

Syntax

Following is the syntax for declaring and initializing a Dictionary −

Dictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
dict.Add(key, value);

Following are the common iteration patterns −

// Using KeyValuePair
foreach (KeyValuePair<TKey, TValue> pair in dict) {
   // Access pair.Key and pair.Value
}

// Using Keys collection
foreach (TKey key in dict.Keys) {
   // Access key and dict[key]
}

// Using Values collection
foreach (TValue value in dict.Values) {
   // Access only values
}

Using KeyValuePair (Recommended)

The most efficient way to iterate over a dictionary is using KeyValuePair, which provides direct access to both key and value −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<int, string> grades = new Dictionary<int, string>();
      grades.Add(1, "A+");
      grades.Add(2, "B+");
      grades.Add(3, "A");
      grades.Add(4, "C+");

      foreach (KeyValuePair<int, string> pair in grades) {
         Console.WriteLine("Student {0}: Grade {1}", pair.Key, pair.Value);
      }
   }
}

The output of the above code is −

Student 1: Grade A+
Student 2: Grade B+
Student 3: Grade A
Student 4: Grade C+

Using Keys Collection

You can iterate through the keys and access values using the indexer −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<int, int> scores = new Dictionary<int, int>();
      scores.Add(1, 97);
      scores.Add(2, 89);
      scores.Add(3, 77);
      scores.Add(4, 88);

      foreach (int key in scores.Keys) {
         Console.WriteLine("Key: {0}, Value: {1}", key, scores[key]);
      }
   }
}

The output of the above code is −

Key: 1, Value: 97
Key: 2, Value: 89
Key: 3, Value: 77
Key: 4, Value: 88

Using Values Collection

When you only need the values, you can iterate through the Values collection −

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      Dictionary<string, double> prices = new Dictionary<string, double>();
      prices.Add("Apple", 1.50);
      prices.Add("Banana", 0.75);
      prices.Add("Orange", 2.00);

      Console.WriteLine("All prices:");
      foreach (double price in prices.Values) {
         Console.WriteLine("${0:F2}", price);
      }
   }
}

The output of the above code is −

All prices:
$1.50
$0.75
$2.00

Comparison of Iteration Methods

Method Access Performance Use Case
KeyValuePair Key + Value Best When you need both key and value
Keys collection Key + Value via indexer Good When you primarily need keys
Values collection Value only Best When you only need values

Conclusion

Dictionary iteration in C# can be accomplished through KeyValuePair (most efficient for key-value access), Keys collection (when keys are primary), or Values collection (when only values are needed). Choose the method that best fits your specific requirements for optimal performance and code clarity.

Updated on: 2026-03-17T07:04:35+05:30

533 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements