VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-performing-specified-action-on-each-element-of-array/

⇱ C# | Performing Specified action on each element of Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# | Performing Specified action on each element of Array

Last Updated : 11 Jul, 2025

Array.ForEach(T[], Action<T>) Method is used to perform the specified action on each element of the specified array.
 

Syntax:  

public static void ForEach<T> (T[] array, Action<T> action);


Parameters: 

array: The one-dimensional, zero-based Array on whose elements the action is to be performed. 
action: The Action to perform on each element of array.  


Exception: This method throws ArgumentNullException array is null or action is null.
Below programs illustrate the use of Array.ForEach(T[], Action) Method:
Example 1: 


Output: 
Initial Array: 2 3 4 5 

2 squared = 4
3 squared = 9
4 squared = 16
5 squared = 25

 

Example 2:


Output: 
Trying to perform action on a null Array

Exception Thrown: System.ArgumentNullException

 

Reference: 

Comment
Article Tags:

Explore