Array.ForEach(T[], Action<T>
) 方法
定义
命名空间:
System
程序集:
mscorlib.dll, System.Runtime.dll
对指定数组的每个元素执行指定操作。
public static void ForEach<T> (T[] array, Action<T> action);
类型参数
T
数组元素的类型。
参数
array T[]
从零开始的一维 Array,要对其元素执行操作。
action Action<T>
要对 array 的每个元素执行的 Action<T>
。
示例
下面的示例演示如何使用 ForEach 来显示整数数组中每个元素的平方。
using System;
public class SamplesArray
{
public static void Main()
{
int[] intArray = new int[] {2, 3, 4};
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);
}
}
/*
代码输出:
2 squared = 4
3 squared = 9
4 squared = 16
*/