Array.ForEach<T>(T[], Action<T>) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Performs the specified action on each element of the specified array.
public:
generic <typename T>
static void ForEach(cli::array <T> ^ array, Action<T> ^ action);
public static void ForEach<T> (T[] array, Action<T> action);
static member ForEach : 'T[] * Action<'T> -> unit
Public Shared Sub ForEach(Of T) (array As T(), action As Action(Of T))
- T
The type of the elements of the array.
- array
- T[]
The one-dimensional, zero-based Array on whose elements the action is to be performed.
The following example shows how to use ForEach to display the squares of each element in an integer array.
using namespace System;
public ref class SamplesArray
{
public:
static void Main()
{
// create a three element array of integers
array<int>^ intArray = gcnew array<int> {2, 3, 4};
// set a delegate for the ShowSquares method
Action<int>^ action = gcnew Action<int>(ShowSquares);
Array::ForEach(intArray, action);
}
private:
static void ShowSquares(int val)
{
Console::WriteLine("{0:d} squared = {1:d}", val, val*val);
}
};
int main()
{
SamplesArray::Main();
}
/*
This code produces the following output:
2 squared = 4
3 squared = 9
4 squared = 16
*/
using System;
public class SamplesArray
{
public static void Main()
{
// create a three element array of integers
int[] intArray = new int[] {2, 3, 4};
// set a delegate for the ShowSquares method
Action<int> action = new Action<int>(ShowSquares);
Array.ForEach(intArray, action);
}
private static void ShowSquares(int val)
{
Console.WriteLine("{0:d} squared = {1:d}", val, val*val);
}
}
/*
This code produces the following output:
2 squared = 4
3 squared = 9
4 squared = 16
*/
open System
let showSquares val' =
printfn $"%i{val'} squared = %i{val' * val'}"
// create a three element array of integers
let intArray = [| 2..4 |]
Array.ForEach(intArray, showSquares)
// Array.iter showSquares intArray
// This code produces the following output:
// 2 squared = 4
// 3 squared = 9
// 4 squared = 16
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
The Action<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<T>.
This method is an O(n
) operation, where n
is the Length of array
.
In F#, the Array.iter function can be used instead.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: