---
title: "Foreach Loop in C#"  
description: "Foreach Loop in C#"  
author: "Ravi Vishwakarma"  
published: 2024-06-09  
updated: 2024-06-09  
canonical: https://www.mindstick.com/interview/33898/foreach-loop-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 3 minutes  

---

# Foreach Loop in C#

The foreach loop in C# is used to iterate over a collection of items, such as **arrays**, **lists**, or other **enumerable** **collections**. It is beneficial for iterating through elements without knowing the **collection size** or managing loop **counters**. The foreach loop provides a cleaner and more readable way to traverse collections than the traditional for loop.

Here is the basic syntax of a foreach loop in C#:

```cs
foreach (var item in collection)
{
    // Code to execute for each item
}
```

### Example Usage

## Iterating Over an Array:

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

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

## Iterating Over a List:

```cs
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };

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

### Key Points

- The `foreach` loop can only be used with collections that implement the `IEnumerable` interface.
- The loop variable (e.g., `item` in the examples) is read-only within the scope of the loop. You cannot assign a new value to `item` within the loop body.
- The `foreach` loop automatically handles the iteration bounds, so you do not need to manage index variables manually.
- It is generally safer and less error-prone than using a `for` loop with index variables, especially when working with collections that can change in size or when the iteration logic is complex.

### Limitations

- You cannot modify the collection itself (e.g., **adding** or **removing elements**) while iterating over it using a `foreach` loop. Doing so will result in an `InvalidOperationException`.
- If you need to modify elements of a collection, consider using a `for` loop or other appropriate methods that provide access to the collection by index.

## Answers

### Answer by Ravi Vishwakarma

The foreach loop in C# is used to iterate over a collection of items, such as **arrays**, **lists**, or other **enumerable** **collections**. It is beneficial for iterating through elements without knowing the **collection size** or managing loop **counters**. The foreach loop provides a cleaner and more readable way to traverse collections than the traditional for loop.

Here is the basic syntax of a foreach loop in C#:

```cs
foreach (var item in collection)
{
    // Code to execute for each item
}
```

### Example Usage

## Iterating Over an Array:

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

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

## Iterating Over a List:

```cs
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };

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

### Key Points

- The `foreach` loop can only be used with collections that implement the `IEnumerable` interface.
- The loop variable (e.g., `item` in the examples) is read-only within the scope of the loop. You cannot assign a new value to `item` within the loop body.
- The `foreach` loop automatically handles the iteration bounds, so you do not need to manage index variables manually.
- It is generally safer and less error-prone than using a `for` loop with index variables, especially when working with collections that can change in size or when the iteration logic is complex.

### Limitations

- You cannot modify the collection itself (e.g., **adding** or **removing elements**) while iterating over it using a `foreach` loop. Doing so will result in an `InvalidOperationException`.
- If you need to modify elements of a collection, consider using a `for` loop or other appropriate methods that provide access to the collection by index.


---

Original Source: https://www.mindstick.com/interview/33898/foreach-loop-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
