In C#, List.ForEach(action) calls a small function once for every item, from the first item to the last. The function is an Action<T>, which takes one item and returns nothing, usually written as a lambda such as n => Console.WriteLine(n).
Do not mix it up with the foreachstatement. The statement works on any collection and supports break, continue, and return. The ForEachmethod belongs to List<T> only, and you cannot stop it early. If the action changes the list (adds or removes items), you get an InvalidOperationException.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.ForEach(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass a lambda that does something with one item. Nothing is returned.
C#
List<int> numbers = new List<int> { 1, 2, 3 };
numbers.ForEach(n => Console.WriteLine(n * 10)); // 10, 20, 30
names.ForEach(Console.WriteLine); // pass a method name
Call
What it does
numbers.ForEach(n => Console.WriteLine(n))
Prints each number on its own line.
names.ForEach(Console.WriteLine)
Pass a method name when it already has the right shape.
numbers.ForEach(n => total += n)
A lambda may update a variable that is declared outside it.
ForEach is O(n). The lambda receives a copy of each item, so assigning to the parameter (n = n * 2) does not change a List<int>. For classes, the copy still points to the same object, so changing its fields does work. A null action throws ArgumentNullException. Array.ForEach is the static version for arrays.
Library declarations
C#
public void ForEach(Action<T> action);
Parameters
Parameter
Description
action
Required. An Action<T>: a lambda or method that takes one item and returns nothing. It must not be null.
Return Value
Return Value
Description
void
Nothing is returned. The action has run once for each item, in order.
C# Examples
Example 1 — Run an Action for Every Item
Print each number doubled, then print each name by passing a method name.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3 };
numbers.ForEach(n => Console.WriteLine(n * 2));
List<string> names = new List<string> { "Asha", "Ben" };
names.ForEach(Console.WriteLine);
}
}
Output
2
4
6
Asha
Ben
How It Works
One call per item. The lambda n => Console.WriteLine(n * 2) runs for 1, then 2, then 3, in list order.
Action, not Func. The function returns nothing (void), so there is no result to store from ForEach.
Method name instead.Console.WriteLine accepts one string, which is exactly what an Action<string> needs, so it can be passed directly.
Stable order. The items are visited from first to last every time, so the output never changes.
Example 2 — Add Up Items and Learn What Does Not Work
A lambda can update a variable outside it. Assigning to its own parameter does nothing to the list.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 5, 10, 15 };
int total = 0;
numbers.ForEach(n => total += n);
Console.WriteLine(total);
numbers.ForEach(n => n = n * 2);
Console.WriteLine(string.Join(", ", numbers));
}
}
Output
30
5, 10, 15
How It Works
Captured variable. The lambda can read and change total because it is declared outside. After the call it holds 5 + 10 + 15.
Parameter is a copy. In n => n = n * 2, n is a local copy of each number. Changing it does not write back into the list.
List unchanged. The second line shows 5, 10, 15, not the doubled values.
To change items. Use a for loop with numbers[i] = ..., or build a new list with ConvertAll.
Example 3 — Changing the List Inside ForEach
Adding an item from inside the action throws an exception. Use a second list to collect the new items instead.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> list = new List<int> { 1, 2, 3 };
try {
list.ForEach(n => {
if (n == 2) {
list.Add(99);
}
});
} catch (InvalidOperationException) {
Console.WriteLine("list changed while looping");
}
List<int> extra = new List<int>();
list.ForEach(n => extra.Add(n * 10));
Console.WriteLine(string.Join(", ", extra));
}
}
Output
list changed while looping
10, 20, 30, 990
How It Works
Edit during the loop. When n is 2, the action calls list.Add(99). The list notices that it was changed during enumeration.
Exception.ForEach throws InvalidOperationException, and the catch block prints the message. The Add already happened, so the list now has four items.
Use a helper list. The second ForEach only reads list and writes into a different list, extra, so it is allowed.
Result. The list now holds 1, 2, 3, 99, so extra receives 10, 20, 30, 990.
Frequently Asked Questions
It runs your lambda once for every item in the list, from first to last. It returns nothing.
foreach is a language statement that works on any collection and supports break and continue. List.ForEach is a method on List<T> and Array only, takes a lambda, and cannot be stopped early.
No. Changing the list from inside the action throws InvalidOperationException. Collect changes in another list, or use RemoveAll.
Not for simple values. The lambda gets a copy of each value. For classes, you can change the fields of the object, because the copy points to the same object.
There is no real difference for most programs. Use the one that reads more clearly. The statement is more flexible because of break and continue.