Array.ForEach(Of T) Method (T(), Action(Of T))
.NET Framework (current version)
Performs the specified action on each element of the specified array.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- array
-
Type:
T()
The one-dimensional, zero-based Array on whose elements the action is to be performed.
- action
-
Type:
System.Action(Of T)
The Action(Of T) to perform on each element of array.
Type Parameters
- T
The type of the elements of the array.
| Exception | Condition |
|---|---|
| ArgumentNullException | array is null. -or- action is null. |
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
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Show: