List<T>::ForEach Method (Action<T>^)
Performs the specified action on each element of the List<T>.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition | ||
|---|---|---|---|
| ArgumentNullException | action is null. | ||
| InvalidOperationException | An element in the collection has been modified.
|
The Action<T> is a delegate to a method that performs an action on the object passed to it. The elements of the current List<T> are individually passed to the Action<T> delegate.
This method is an O(n) operation, where n is Count.
Modifying the underlying collection in the body of the Action<T> delegate is not supported and causes undefined behavior.
The following example demonstrates the use of the Action<T> delegate to print the contents of a List<T> object. In this example the Print method is used to display the contents of the list to the console.
Note |
|---|
In addition to displaying the contents using the Print method, the C# example demonstrates the use of to display the results to the console. |
Available since 10
.NET Framework
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

