List<T>.ForEach Method
Performs the specified action on each element of the List<T>.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| ArgumentNullException | action is null. |
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 anonymous methods to display the results to the console. |
using System; using System.Collections.Generic; class Program { static void Main() { List<String> names = new List<String>(); names.Add("Bruce"); names.Add("Alfred"); names.Add("Tim"); names.Add("Richard"); // Display the contents of the list using the Print method. names.ForEach(Print); // The following demonstrates the anonymous method feature of C# // to display the contents of the list to the console. names.ForEach(delegate(String name) { Console.WriteLine(name); }); } private static void Print(string s) { Console.WriteLine(s); } } /* This code will produce output similar to the following: * Bruce * Alfred * Tim * Richard * Bruce * Alfred * Tim * Richard */
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: