This documentation is archived and is not being maintained.
Array::ForEach<T> Method
Visual Studio 2010
Performs the specified action on each element of the specified array.
Assembly: mscorlib (in mscorlib.dll)
Type Parameters
- T
The type of the elements of the array.
Parameters
- array
- Type: array<T>
The one-dimensional, zero-based Array on whose elements the action is to be performed.
- action
- Type: System::Action<T>
The Action<T> to perform on each element of array.
| Exception | Condition |
|---|---|
| ArgumentNullException | array is nullptr. -or- action is nullptr. |
The following example shows how to use ForEach<T> 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 */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: