How to iterate through a Dictionary in C# with foreach
This tutorial explains how to iterate over a Dictionary in C# using the foreach statement. The foreach statement can be used to iterate over key and value, only key, only value, etc. You can also get the index in the iteration process.
If you are interested in how to iterate over a Dictionary in C# using foreach, please take a look.
Learn more:
Basic iteration
This section shows how to iterate through a Dictionary in C# using foreach. Let's take a look at the sample code:
using System; using System.Collections.Generic; namespace CSharpExample { class Program { static void Main(string[] args) { //Initiliazing a dictionary Dictionary<string, string> books = new Dictionary<string, string> { {"ID01", "Title 01"}, {"ID02", "Title 02"}, {"ID03", "Title 03"}, {"ID04", "Title 04"}, }; Console.WriteLine("Obtaining keys and values:"); //Obtaining keys and values foreach (KeyValuePair<string, string> book in books) { Console.WriteLine("Key: {0}, Value: {1}", book.Key, book.Value); } Console.WriteLine("Obtaining only keys:"); //Obtaining keys foreach (string key in books.Keys) { Console.WriteLine("Key: {0}", key); } Console.WriteLine("Obtaining only values:"); //Obtaining values foreach (string value in books.Values) { Console.WriteLine("Value: {0}", value); } } } }
The execution results are as follows:
Obtaining keys and values:
Key: ID01, Value: Title 01
Key: ID02, Value: Title 02
Key: ID03, Value: Title 03
Key: ID04, Value: Title 04
Obtaining only keys:
Key: ID01
Key: ID02
Key: ID03
Key: ID04
Obtaining only values:
Value: Title 01
Value: Title 02
Value: Title 03
Value: Title 04
Obtaining the index
When iterating through a Dictionary in C# with foreach, here is how to get the index. Let's take a look at the sample code:
using System; using System.Collections.Generic; using System.Linq; namespace CSharpExample { class Program { static void Main(string[] args) { //Initiliazing a dictionary Dictionary<string, string> books = new Dictionary<string, string> { {"ID01", "Title 01"}, {"ID02", "Title 02"}, {"ID03", "Title 03"}, {"ID04", "Title 04"}, {"ID05", "Title 05"}, }; Console.WriteLine("Obtaining keys, values, and index:"); //Obtaining keys, values, and index var items = books.Select((Entry, Index) => new { Entry, Index }); foreach (var item in items) { Console.WriteLine("Index: {0}, Key: {1}, Value: {2}", item.Index, item.Entry.Key, item.Entry.Value); } } } }
Output:
Obtaining keys, values, and index:
Index: 0, Key: ID01, Value: Title 01
Index: 1, Key: ID02, Value: Title 02
Index: 2, Key: ID03, Value: Title 03
Index: 3, Key: ID04, Value: Title 04
Index: 4, Key: ID05, Value: Title 05
Deletion in the iterative process
In C#, what happens when an element is deleted while iterating through a Dictionary with foreach?
When the above is executed, System.InvalidOperationException is raised. Deletion or other operations are not allowed during the iterative process of the collection.
To solve this, convert the Dictionary keys to a List, iterate through the List with foreach, and delete the Dictionary elements. Let's take a look at the sample code:
using System; using System.Collections.Generic; using System.Linq; namespace CSharpExample { class Program { static void Main(string[] args) { //Initiliazing a dictionary Dictionary<string, string> books = new Dictionary<string, string> { {"ID01", "Title 01"}, {"ID02", "Title 02"}, {"ID03", "Title 03"}, {"ID04", "Title 04"}, {"ID05", "Title 05"}, }; foreach (string key in books.Keys) { //Remove if key = ID03 if (key == "ID03") { books.Remove(key); } } foreach (KeyValuePair<string, string> book in books) { Console.WriteLine("Key: {0}, Value: {1}", book.Key, book.Value); } } } }
Output:
Key: ID01, Value: Title 01
Key: ID02, Value: Title 02
Key: ID04, Value: Title 04
Key: ID05, Value: Title 05
Updating in the iterative process
In C#, here is how to update a Dictionary with foreach. Let's take a look at the sample code:
using System; using System.Collections.Generic; using System.Linq; namespace CSharpExample { class Program { static void Main(string[] args) { //Initiliazing a dictionary Dictionary<string, string> books = new Dictionary<string, string> { {"ID01", "Title 01"}, {"ID02", "Title 02"}, {"ID03", "Title 03"}, {"ID04", "Title 04"}, {"ID05", "Title 05"}, }; foreach (string key in books.Keys.ToList()) { //Update if key = ID04 if (key == "ID04") { books[key] = "Title 40"; } } foreach (KeyValuePair<string, string> book in books) { Console.WriteLine("Key: {0}, Value: {1}", book.Key, book.Value); } } } }
Output:
Key: ID01, Value: Title 01
Key: ID02, Value: Title 02
Key: ID03, Value: Title 03
Key: ID04, Value: Title 40
Key: ID05, Value: Title 05
Summary
I hope you enjoyed this tutorial on how to iterate over a Dictionary in C# using foreach. The foreach loop can be used to iterate over keys and values, only keys, only values. You can also use iteration to obtain an index.
Back to a list of C# tutorials