
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - ForEach() Method
The C# Array ForEach() method is used to perform the specified action on each element of an array.
The action is a lambda expression that performs an action on the object given to it. The elements of the array are passed individually to the Action<T>.
Syntax
Following is the syntax of the C# Array ForEach() method −
public static void ForEach<T> (T[] array, Action<T> action);
Parameters
This method accepts the following parameters −
- array: The one dimensional zero based array on whose element action to be performed.
- action: The action to be performed on each element of array.
Return value
This method does not return any value.
Example 1: Display Array Element
This is the basics example that shows how to use ForEach() to display each element in the array −
using System; class Program { static void Main() { int[] numbers = { 1, 2, 3, 4, 5 }; // Display each number in the array Array.ForEach(numbers, number => Console.WriteLine(number)); } }
Output
Following is the output −
1 2 3 4 5
Example 2: Multiply Each Element By 2
Let's create an example that displays each element multiplied by 2. We use the ForEach() method to perform the calculation of the each element −
using System; class Program { static void Main() { int[] numbers = { 1, 2, 3, 4, 5 }; // mutliplay each number by 2 and print them Array.ForEach(numbers, number => Console.WriteLine(number * 2)); } }
Output
Following is the output −
2 4 6 8 10
Example 3: Modify Object Property
Let's see another example of the ForEach() method, Here we will manipulate the object's property of the person −
using System; class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main() { Person[] people = { new Person { Name = "Jessica", Age = 25 }, new Person { Name = "Luna", Age = 27 }, new Person { Name = "Amber", Age = 33 } }; // Increment each person's age by 2 Array.ForEach(people, person => person.Age += 2); // Display updated ages Array.ForEach(people, person => Console.WriteLine($"{person.Name}: {person.Age}")); } }
Output
Following is the output −
Jessica: 27 Luna: 29 Amber: 35
Example 4: Calculate the Square of Each Element
The below example uses the ForEach() method to perform an action to calculate the square of each element in the array −
using System; public class SamplesArray { public static void Main() { 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); } }
Output
Following is the output −
2 squared = 4 3 squared = 9 4 squared = 16