Array.ForEach<T>(T[], Action<T>) Metoda

Definice

Provede zadanou akci pro každý prvek zadaného pole.

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))

Parametry typu

T

Typ prvků pole.

Parametry

array
T[]

Jednorozměrná nula založená Array na tom, na jejichž prvcích se akce provádí.

action
Action<T>

Provedení Action<T> pro každý prvek .array

Výjimky

array je null.

-nebo-

action je null.

Příklady

Následující příklad ukazuje, jak použít ForEach k zobrazení čtverců jednotlivých prvků v celočíselném poli.

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

Poznámky

Je Action<T> delegátem metody, která provádí akci objektu předaného do ní. Prvky array jsou jednotlivě předány Action<T>.

Tato metoda je operace O(n), kde n je Length .array

V jazyce F# lze místo toho použít funkci Array.iter .

Platí pro

Viz také