C# Keywords Tutorial Part 36: foreach

C# Keywords Tutorial Part 36: foreach

C# is a programming language that is popularly used for developing different types of applications, including desktop, web, and mobile apps. It is an object-oriented language that incorporates the “foreach” keyword, which makes it simple and effective to iterate over collections like arrays, lists, and dictionaries. This article delves into the “foreach” keyword, providing a detailed explanation along with some code examples.

What is “foreach”?

The “foreach” keyword is a looping construct that simplifies the process of iterating over a collection. It takes an input collection and iterates over each element in the collection, executing a code block for each element. The “foreach” loop works with any collection that implements the IEnumerable interface, including arrays, lists, dictionaries, and more.

Syntax of “foreach”

The syntax of “foreach” is as follows:

foreach (var item in collection)
{
    // code block to execute
}

In the above syntax, “item” represents the current element in the collection, and “collection” represents the collection to iterate over. The code block inside the loop is executed for each element in the collection.

Example 1: Iterating over an array

Let’s start with a simple example of using “foreach” to iterate over an array. Consider the following array of integers:

int[] numbers = { 1, 2, 3, 4, 5 };

To iterate over this array using “foreach”, we can use the following code:

foreach (int number in numbers)
{
    Console.WriteLine(number);
}

In the above code, we are iterating over the “numbers” array and printing each element to the console using the Console.WriteLine() method. The output of this code will be:

1
2
3
4
5

Example 2: Iterating over a list

The “foreach” loop works equally well with other types of collections, such as lists. Consider the following list of strings:

List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Dave", "Emily" };

To iterate over this list using “foreach”, we can use the following code:

foreach (string name in names)
{
    Console.WriteLine(name);
}

In the above code, we are iterating over the “names” list and printing each element to the console using the Console.WriteLine() method. The output of this code will be:

Dictionary<string, int> scores = new Dictionary<string, int>
{
    { "Alice", 90 },
    { "Bob", 80 },
    { "Charlie", 95 },
    { "Dave", 85 },
    { "Emily", 92 }
};

To iterate over this dictionary using “foreach”, we can use the following code:

foreach (KeyValuePair<string, int> score in scores)
{
    Console.WriteLine(score.Key + ": " + score.Value);
}

In the above code, we are iterating over the “scores” dictionary and printing each key-value pair to the console using the Console.WriteLine() method. The output of this code will be:

Alice: 90
Bob: 80
Charlie: 95
Dave: 85
Emily: 92

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *