ForEach Method
.NET Framework Class Library
List<(Of <(T>)>)..::.ForEach Method

Performs the specified action on each element of the List<(Of <(T>)>).

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Sub ForEach ( _
    action As Action(Of T) _
)
Visual Basic (Usage)
Dim instance As List
Dim action As Action(Of T)

instance.ForEach(action)
C#
public void ForEach(
    Action<T> action
)
Visual C++
public:
void ForEach(
    Action<T>^ action
)
JScript
public function ForEach(
    action : Action<T>
)

Parameters

action
Type: System..::.Action<(Of <(T>)>)
The Action<(Of <(T>)>) delegate to perform on each element of the List<(Of <(T>)>).
ExceptionCondition
ArgumentNullException

action is nullNothingnullptra null reference (Nothing in Visual Basic).

The Action<(Of <(T>)>) is a delegate to a method that performs an action on the object passed to it. The elements of the current List<(Of <(T>)>) are individually passed to the Action<(Of <(T>)>) delegate.

This method is an O(n) operation, where n is Count.

The following example demonstrates the use of the Action<(Of <(T>)>) delegate to print the contents of a List<(Of <(T>)>) object. In this example the Print method is used to display the contents of the list to the console.

NoteNote:

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.

Visual Basic
Imports System
Imports System.Collections.Generic

Class Program
    Shared Sub Main()
        Dim names As New List(Of 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(AddressOf Print)
    End Sub

    Shared Sub Print(ByVal s As String)
        Console.WriteLine(s)
    End Sub
End Class

' This code will produce output similar to the following:
' Bruce
' Alfred
' Tim
' Richard
C#
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.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
Page view tracker