In this article, I have mentioned how we can iterate over a dictionary in C# using foreach or using for loop or using ParallelEnumerable.ForAll() with console application example.
Using Foreach
Simplest way to loop dictionary is to use Foreach loop
using System;
using System.Collections.Generic;
namespace IterateDictionary
{
public class Program
{
static void Main(string[] args)
{
Dictionary<int, string> countryNames = new Dictionary<int,string>();
countryNames.Add(1, "India");
countryNames.Add(2, "Australia");
countryNames.Add(3, "United States");
countryNames.Add(4, "United Kingdom");
foreach(var item in countryNames)
{
Console.WriteLine("Key: " + item.Key + " Value:" + item.Value);
}
}
}
}
Output:
Key: 1 Value:India
Key: 2 Value:Australia
Key: 3 Value:United States
Key: 4 Value:United Kingdom
The item
variable in the above code of foreach loop will have KeyValuePair<TKey, TValue>
type.
Using For Loop
You can also use for loop instead of foreach loop in some situations when you need a specific position element, but we will have to use Linq in this method to retrieve dictionary item at a particular positions.
using System;
using System.Collections.Generic;
using System.Linq;
namespace IterateDictionary
{
public class Program
{
static void Main(string[] args)
{
Dictionary<int, string> countryNames = new Dictionary<int,string>();
countryNames.Add(1, "India");
countryNames.Add(2, "Australia");
countryNames.Add(3, "United States");
countryNames.Add(4, "United Kingdom");
for (int i = 0; i < countryNames.Count; i++)
{
var item = countryNames.ElementAt(i);
Console.WriteLine(item.Value);
}
}
}
}
Output:
India
Australia
United States
United Kingdom
Using ParallelEnumerable.ForAll()
ParallelEnumerable.ForAll() can be used if you have a large dictionary, we can make use of Parallel Language Integrated Query (LINQ) query.
using System;
using System.Collections.Generic;
using System.Linq;
namespace IterateDictionary
{
public class Program
{
static void Main(string[] args)
{
Dictionary<int, string> countryNames = new Dictionary<int,string>();
countryNames.Add(1, "India");
countryNames.Add(2, "Australia");
countryNames.Add(3, "United States");
countryNames.Add(4, "United Kingdom");
countryNames.AsParallel().ForAll(country => Console.WriteLine("Key: " + country.Key + " Value:" + country.Value));
}
}
}
Output:
Key: 3 Value:United States
Key: 1 Value:India
Key: 4 Value:United Kingdom
Key: 2 Value:Australia
If you will check above output, you will see it when using AsParallel.ForAll, key/value is printed randomly rather than in sequence.
Best way to loop the dictionary in C#
Even though we have multiple ways to iterate over dictionary key values, prefer using a simple foreach
loop.
If you want to loop only C# dictionary Keys or Values use dictionary.Keys or dictionary.Values instead of an iterating the entire dictionary.
You may also like to read:
Using Generics in C# (With Example)
SingleOrDefault vs FirstOrDefault in C#
Remove last character from string in C#
What is Console.Log Equivalent in C#?
Understanding Delegates in C# with examples (with Multicast delegate)