Partager via


List<T>.ForEach Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Sub ForEach ( _
    action As Action(Of T) _
)
public void ForEach(
    Action<T> action
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

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

Remarks

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.

Examples

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.

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.

Imports System.Collections.Generic

Class Example
   Private Shared outputBlock As System.Windows.Controls.TextBlock
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock
      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)
      outputBlock.Text &= s & vbCrLf
   End Sub
End Class

' This code will produce output similar to the following:
' Bruce
' Alfred
' Tim
' Richard
using System;
using System.Collections.Generic;

class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      Example.outputBlock = outputBlock;
      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.
      names.ForEach(delegate(String name)
      {
         outputBlock.Text += name + "\n";
      });
   }

   private static void Print(string s)
   {
      outputBlock.Text += s + "\n";
   }
}
/* This code will produce output similar to the following:
 * Bruce
 * Alfred
 * Tim
 * Richard
 * Bruce
 * Alfred
 * Tim
 * Richard
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.