Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

List(Of T).ForEach Method

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

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

Public Sub ForEach ( _
	action As Action(Of T) _


Type: System.Action(Of T)

The Action(Of T) delegate to perform on each element of the List(Of T).


action is Nothing.


An element in the collection has been modified.

Caution noteCaution

This exception is thrown starting with the .NET Framework 4.5.

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.

Modifying the underlying collection in the body of the Action(Of T) delegate is not supported and causes undefined behavior.

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.


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 using the Print method.
        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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
© 2015 Microsoft