---
title: "How to iterate through the elements of a collection using a foreach loop in C#?"  
description: "How to iterate through the elements of a collection using a foreach loop in C#?"  
author: "Steilla Mitchel"  
published: 2023-11-03  
updated: 2023-11-05  
canonical: https://www.mindstick.com/forum/160384/how-to-iterate-through-the-elements-of-a-collection-using-a-foreach-loop-in-c-sharp  
category: "c#"  
tags: ["c#", "collection"]  
reading_time: 2 minutes  

---

# How to iterate through the elements of a collection using a foreach loop in C#?

How to iterate through the [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) of a [collection](https://www.mindstick.com/articles/1718/collections-in-java) using a [foreach loop in C#](https://www.mindstick.com/forum/33685/foreach-loop-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

In C#, you can iterate through the elements of a collection, such as a list, array, or other enumerable data structure, using a **foreach** loop. Here's how to do it:

```plaintext
foreach (ElementType element in collection)
{
    // Code to process 'element'
}
```

Here's a step-by-step explanation:

**[foreach Loop](https://www.mindstick.com/forum/182/problem-in-getting-the-value-from-array-by-using-foreach-loop):** Start with the **foreach** keyword, followed by parentheses containing the loop variables and the collection to be iterated.

**ElementType:** Replace "ElementType" with the actual data type of the elements within the collection. This is the type of variable that will represent each element during iteration.

**element:** Choose a variable name (e.g., **element**) that will represent each element as you iterate through the collection. This variable is declared implicitly.

**collection:** Replace "collection" with the name of the collection you want to iterate through.

Here's an example of how to use a **foreach** loop to iterate through the elements of a list:

```plaintext
using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

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

In this example, a **List<int>** named **numbers** is created and populated with integers. The **foreach** loop iterates through the elements of the list, and the variable **number** represents each element in turn. The code inside the loop body prints each element to the console.

You can use a **foreach** loop to iterate through various types of collections, such as arrays, lists, dictionaries, sets, and custom data structures that implement the **IEnumerable** or **IEnumerable<T>** interface. The **foreach** loop simplifies the process of iterating through elements and is widely used for working with collections in C#.


---

Original Source: https://www.mindstick.com/forum/160384/how-to-iterate-through-the-elements-of-a-collection-using-a-foreach-loop-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
