c sharp

C# ForEach Dictionary

In C# programming, the dictionary collects keys and values. It is a generic collection and is present in the “System.Collections.Generic” namespace. The dictionary is dynamic. The dictionary can adjust its size according to the necessity. We must have a unique key. The key value is not duplicated or null. In C# programming, we have different methods to iterate over the dictionary. The foreach loop method is also used for iterating over the dictionary. This article will discuss how to iterate over a dictionary using a foreach loop. We have different examples here in which we use foreach to iterate over the dictionary.

Example 1

We are going to elaborate on an example in Ubuntu 20.04. First, we have to open the text editor of Ubuntu 20.04 and perform this example on it, and after completing this code, we must save our file by using the “.cs” file extension. We use the foreach loop in this example to loop through the dictionary and render all the keys and values of the dictionary on the screen.

Initially, we import the C# library, which is “using System” and then have the “System.Collections.Generic” namespace in which the generic collection classes are available. As we also know, that dictionary in the C# programming is the generic collection. So we get access to this by using the namespace “System.Collections.Generic” with the “using” keyword. We define a class as “CSharp” and then we use the “main” function. We create an instance of the Dictionary.

The name of this instance is “record”. This “Dictionary” is present in the “System.Collections.Generic” namespace. Now, let’s pass the “Key” and “Value”. Here, we give the data type of the “Key” and “Value” which is “string” and “int” respectively. We use the “new” keyword for creating this instance. Now, we have to add keys and values to this dictionary. We add “John” as a “Key” and “19” as “Value”. Same, we add “David”, “Alies”, and “Peter”, as the “Keys” and also “77”, “53”, and “43” as “Values” in the dictionary.

After adding these values to the dictionary, we render a line on the screen using the “Console.WriteLine”. Then, we want to access all these keys and values, so we have to use the “foreach” loop for this. This loop will iterate over the dictionary, get the “keys” and “values,” and store them in pairs. In this “foreach” loop, the “KeyValuePair” is used, which is the data structure, and we use this for holding two associated items. This “foreach” loop works as it takes both the key and value of the dictionary named “record” and stores it in “pair”. The “Console.WriteLine” displays the “key” and “value” which are stored in “pair” with the help of “pair.key” and “pair.value” respectively.

We have to use the commands for displaying the output in Ubuntu 20.04. To run the commands, you have to open the terminal and then write the “mcs” command that compiles the code and makes an executable file. The output is here, and you can see that it displays all the “keys” and “values” of the dictionary on the terminal of Ubuntu 20.04. So, in this way, we can loop over the dictionary and display all the “keys” and “values” of the dictionary on the terminal.

Example 2

Let’s change the program and use the same “foreach” loop for iterating over the dictionary.

We must import the “System” library in our C# code for importing the classes and functions used in our C# program. As we write this code for iterating over the C# dictionary, we must import the “System.Collections.Generic” namespace for the dictionary. We will start a public class with the name “Demo”. We have a “PrintDict” function with a “key” and “value” of the dictionary named “dict”. We create this dictionary “dict” below in this “main” function. The “foreach” loop here will work the same as it works in our previous example.

As we discussed that the “KeyValuePair” is used for holding two related items. We also use this “KeyValuePair” in this “foreach” loop. It holds the key and also the value of the “dict” dictionary and saves them in the “record”. In “Console.WriteLine” the “record.key” will print the “keys” and “record.Value” will print the values of the “dict” dictionary. Now, import the public static void Main() function. In this, we build a dictionary “dict” in which we have “key” of “string” data type, and the “values” are of “int” data type. And use the “new” keyword, as we use in our first example for creating the instance of the dictionary. Then store “Tea”, “Milk”, and “Juice” as “keys” in the dictionary and “50”, “30”, and “60” as the “values” in the dictionary. At the end of the program, we will call the “PrintDict” function and pass the “dict” dictionary instance as a parameter.

After the execution of the code, the given output is rendered. Check the image below, and you will see how this code works. It displays both keys and values in the same way as we discuss in the code.

Example 3

Let’s try another new example which is here in the image given below. We change our code a little bit and use “var” instead of “KeyValuePair” in the “foreach” loop.

We declare a class using the name “Program” and invoke our “main” method. Then create an instance “dic” of the dictionary with the same method as we used in our previous example but here, both the “key” and “value” are of the same “string” data type. We add items to this instance of the dictionary by using the “Add” method. This “Add” method adds all string keys and string values to the dictionary, as shown in the image above. We add “John Alies”, “Micheal”, “James Henry”, and “William James” as “keys” of the dictionary. And also “Programmer”, “Project Manager”, “Project Analyst” and “Tester” as the “values”.

Then for displaying a statement, we use this “Console.WriteLine” in the next line after adding keys and values. Then use the “foreach” loop, which we discussed in detail in the C# codes before this. Here, we use “var” instead of the “KeyValuePair”. And the foreach loop iterates and stores the “dic” key and value in “rec”. Then we display all these on the terminal by “Console.WriteLine”. The “rec.key” displays “keys” and the “rec.value” displays values of “dic”. See the given terminal screen for the output.


We demonstrate this article for you to learn how to use foreach a loop in C# programming. This “foreach” is used in the C# dictionary to loop or iterate over the dictionary. We have elaborated on three examples in this article and explained each C# code. We have demonstrated all the examples in Ubuntu 20.04. This article helps you learn this concept, and you can learn the foreach concept for looping over the dictionary after studying this article. I expect that the given article gives you a well understanding of the concept of the “foreach” dictionary in C# programming.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.