Export (0) Print
Expand All

Array.ForEach(Of T) Method

Performs the specified action on each element of the specified array.

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

Public Shared Sub ForEach(Of T) ( _
	array As T(), _
	action As Action(Of T) _

Type Parameters


The type of the elements of the array.


Type: T()

The one-dimensional, zero-based Array on whose elements the action is to be performed.

Type: System.Action(Of T)

The Action(Of T) to perform on each element of array.


array is Nothing.


action is Nothing.

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

This method is an O(n) operation, where n is the Length of array.

The following example shows how to use ForEach(Of T) to display the squares of each element in an integer array.

Imports System

Public Class SamplesArray
    Public Shared Sub Main()
        ' create a three element array of integers 
        Dim intArray() As Integer = New Integer() {2, 3, 4}

        ' set a delegate for the ShowSquares method 
        Dim action As New Action(Of Integer)(AddressOf ShowSquares)

        Array.ForEach(intArray, action)
    End Sub 

    Private Shared Sub ShowSquares(val As Integer)
        Console.WriteLine("{0:d} squared = {1:d}", val, val*val)
    End Sub 
End Class 

' This code produces the following output: 

' 2 squared = 4 
' 3 squared = 9 
' 4 squared = 16

.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

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft