List.ForEach Method

Performs the specified action on each element of the List.

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)

Public Sub ForEach ( _
	action As Action(Of T) _
Dim instance As List(Of T)
Dim action As Action(Of T)

public void ForEach (
	Action<T> action
public function ForEach (
	action : Action<T>
Not applicable.



The Action delegate to perform on each element of the List.

Exception typeCondition


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

The Action is a delegate to a method that performs an action on the object passed to it. The elements of the current List are individually passed to the Action delegate.

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

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


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.

Imports System
Imports System.Collections.Generic

Class Program
    Shared Sub Main()
        Dim names As New List(Of String)

        ' Display the contents of the List out imports the "Print" delegate.
        names.ForEach(AddressOf Print)
    End Sub

    Shared Sub Print(ByVal s As String)
    End Sub
End Class

' This code will produce output similar to the following:
' Bruce
' Alfred
' Tim
' Richard

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0