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

定义

对指定数组的每个元素执行指定操作。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[]

从零开始的一维 Array,要对其元素执行操作。The one-dimensional, zero-based Array on whose elements the action is to be performed.

action
Action<T> Action<T> Action<T> Action<T>

要对 array 的每个元素执行的 Action<T>The Action<T> to perform on each element of array.

异常

arraynullarray is null.

- 或 --or- actionnullaction is null.

示例

下面的示例演示如何使用ForEach来显示整数数组中每个元素的平方。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
*/

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

注解

Action<T>是对传递给它的对象执行操作的方法的委托。The Action<T> is a delegate to a method that performs an action on the object passed to it. 的元素array分别传递Action<T>到。The elements of array are individually passed to the Action<T>.

此方法为 On() 运算, 其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

适用于

另请参阅