Skip to content

Some C# Foreach Examples and How to use it

C# Foreach Examples

Understanding the C# foreach Loop – C# Foreach Examples

In this article, we will dive deep into the world of the C# foreach loop and understand its capabilities and usage. We will learn how to use the foreach loop to iterate through different types of collections and perform different actions on each element of the collection. Not only that, but we will also explore some practical and useful C# Foreach Examples to see the foreach loop in action. By the end of this article, you will have a solid understanding of the foreach loop and the confidence to start using it in your projects. So, whether you’re a beginner or an experienced developer looking to improve your skills, this article will provide you with all the information you need to get started with the foreach loop in C#.

Theforeach loop in C# is a powerful tool that allows you to iterate through a collection of items, such as an array a list, dictionaries, and even strings. This is a very useful iterative method that you can use to perform specific tasks on each element of a collection. You can use it to print out each element in a collection, search for specific elements, or modify elements within a collection. Here are a few examples of the foreach loop in action.

FLASH SALES
$29.95
NEW
$249.00
Become a job-ready C# developer!

Some C# Foreach Examples

  1. Using foreach to iterate through an array of integers and print each element:
  1. Using foreach to iterate through a dictionary and print each key-value pair:
  1. Using foreach to iterate through a string and print each character:

With these examples in mind, let’s move on to the step-by-step guide on how to use the foreach loop in C# and create a small project to demonstrate its capabilities:

Setting up the project

To begin, we’ll need to create a new C# console application in Visual Studio.

  • Open Visual Studio and click on “Create a new project.”

C# Foreach Examples

  • Select “Console App (.NET 6)” from the list of templates.

C# Foreach Examples

  • Give your project a name, for example “ForeachExamples” and click on “Create.”

C# Foreach Examples

A “PERSON” class

Once the project is created, we’ll add a new class called “Person” to our project. In this class, we’ll have a few properties such as name, age, and address.

 

Creating a list of people

Next, we’ll create a list of people in our main program. We’ll use the List<T> class to create the list, and we’ll add a few instances of the Person class to the list.

 

Using the foreach loop

Now we’re ready to use the foreach loop. We’ll use the foreach loop to iterate through the list of people and print out their name, age, and address. We’ll also use the foreach loop to calculate the average age of the people in the list.

The foreach loop is a type of loop that allows you to iterate through a collection, such as an array or a list. The basic syntax of the foreach loop is as follows:

In this case, the foreach loop iterates through the people list and assigns each item in the list to the variable person. Then, it executes the code inside the loop block, which in this case is printing out the name, age, and address of the person. Additionally, it adds the age of the person to the variable totalAge.

 

Showing the results

Finally, we’ll print out the results of our calculations. We’ll print out the name, age, and address of each person in the list, as well as the average age of the people in the list.

In this last step, we use the totalAge variable and the Count property of the List to calculate the average age of the people in the list. The Console.WriteLine method is used to display the average age on the console. Finally, we use the Console.ReadLine() method to keep the console open until the user presses enter.

 

Conclusion: C# Foreach Examples

The foreach loop in C# is a powerful tool that allows you to easily iterate through a collection of items and perform actions on each item. You can use it to iterate through lists, arrays, and other types of collections. With the foreach loop, it’s easy to perform repetitive tasks on each element of a collection, making your code more efficient and less prone to errors.

As we’ve seen in the examples before step 1, foreach can be used to iterate through different types of collections, like arrays, lists, dictionaries, and even strings. You can use it to print out each element in a collection, search for specific elements, or modify elements within a collection.
 
I hope this article has helped you understand how to use the foreach loop in C#, and as always, if you have any questions, please don’t hesitate to ask. If you’re new to C# and collections, it’s always a good idea to start with basic examples and then build on them as you gain more experience.
 
And don’t forget that when in doubt, you can always refer to this article to help you understand the foreach iterative method and liked these C# Foreach Examples. Also, if you liked this article, you may be interested in some LINQ. Have you ever heard about LINQ? Well, just know that as a C# developer, you must have this knowledge! So go check this article out!
 
If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.

Leave a Reply

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