Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Array foreach: Syntax and Usage

Learn how to use foreach with arrays in C#: syntax, behavior, limitations, and performance considerations with practical code examples.

~3 min readC#
#C##foreach#arrays#iteration
Diagram showing a foreach loop iterating over an array in C# with elements being processed sequentially.

Using foreach with arrays in C# reads each element in order without requiring an index. Here is the basic form:

csharp
int[] numbers = { 10, 20, 30, 40 }; foreach (int number in numbers) { Console.WriteLine(number); }

This article explains how that loop behaves with arrays, when to choose it instead of for, and what to watch out for.

How foreach Works with Arrays

Arrays implement IEnumerable, and single-dimensional arrays also implement IEnumerable<T>. foreach can use a GetEnumerator method, but for a single-dimensional array the compiler can avoid the enumerator and emit an index-based loop because the array length is fixed for the lifetime of the array.

The loop variable is read-only: you cannot assign a new value to number inside the loop. For a value type, the variable is a copy, so modifying it does not change the array. For a reference type, you can call methods on the underlying object, but you cannot replace the reference in the loop variable.

foreach vs for Loop: Choosing the Right Tool

The for loop gives explicit index control and lets you write to specific positions, iterate in reverse, or use a custom step. foreach is more concise and avoids off-by-one errors when you only need to read every element in order.

csharp
// for loop with index for (int i = 0; i < numbers.Length; i++) { numbers[i] = numbers[i] * 2; } // foreach loop (read-only) foreach (int number in numbers) { Console.WriteLine(number); }

Use foreach unless you need the index or need to write back to the array by position.

Modifying Array Elements During Iteration

foreach does not prevent you from modifying an element through the array reference. Because arrays have a fixed size, the structure cannot change while you are iterating. You can write through the array index:

csharp
string[] names = { "Alice", "Bob", "Carol" }; foreach (string name in names) { if (name == "Bob") { names[1] = "Bobby"; // allowed, but an indexed loop is clearer here } }

If you tried to write name = "Bobby";, the compiler would reject it because the loop variable is read-only. To change values by position, an explicit for loop is usually clearer.

Using Array.ForEach and LINQ Alternatives

The static Array.ForEach method applies an Action<T> to each element:

csharp
Array.ForEach(numbers, number => Console.WriteLine(number));

It is more compact for a single expression, but a foreach loop is usually clearer once the body has more than one statement.

LINQ does not provide a ForEach extension for arrays. To transform or filter an array, use Select, Where, and ToArray; this requires using System.Linq;:

csharp
var doubled = numbers.Select(n => n * 2).ToArray();

Use Array.ForEach for side-effect-oriented actions. Use LINQ when you are building a new sequence from the array.

Performance Considerations

For single-dimensional arrays, the C# compiler lowers foreach to an index-based loop, so it does not allocate an enumerator object. For read-only traversal, foreach is normally comparable to for; choose based on whether you need the index, and measure if performance matters.

A foreach over a multi-dimensional array can be slower because the runtime has to track indices for each dimension. If performance is important, consider using nested for loops or flattening the data.

Common Pitfalls and Edge Cases

Iterating over a null array throws a NullReferenceException. Check for null if the array may be uninitialized:

csharp
if (array != null) { foreach (var item in array) { // body } }

An empty array is safe to iterate; the loop body simply never runs.

For jagged arrays, foreach iterates over the inner arrays, not the individual elements. Use nested loops to reach the values:

csharp
int[][] jagged = new int[2][]; jagged[0] = new[] { 1, 2 }; jagged[1] = new[] { 3, 4, 5 }; foreach (int[] inner in jagged) { foreach (int value in inner) { Console.WriteLine(value); } }

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.