Pro EP 17 : Multiple ways to iterate over Dictionary in C#

Muhammad Waseem
Become .NET Pro !
2 min readMar 1, 2023

1. Iterate via for each loop
This one is most simple and widely used approach and its best performance wise as well.

2. Using for loop and iterating over index
In this we iterate over the dictionary index and then use ElementAt method to retrieve key and value.

3. Using AsParallel method
Using this method we can iterate over the dictionary , code seems simpler but it does not display the records in same order as displayed.

Let’s create a dictionary

Method 1

Method 2

Method 3

If you want to help the author in growing

  1. Subscribe my Weekly .NET Newsletter of C#/.NET
  2. Follow me on LinkedIn/Twitter OR Clap at least 50 times.
  3. Get exclusive .NET Questions & Answers by becoming a Patron , I add almost 25+ new questions with explanation per month.
  4. Download my eBook at Gum road that contains 30+ .NET Tips.

--

--