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.

Using foreach with arrays in C# reads each element in order without requiring an index. Here is the basic form:
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.
// 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:
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:
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;:
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:
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:
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 toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.