C# | How to perform a specified action on each element of the List
List<T>.ForEach(Action<T>) Method is used to perform a specified action on each element of the List<T>.
Properties of List:
- It is different from the arrays. A list can be resized dynamically but arrays cannot.
- List class can accept null as a valid value for reference types and it also allows duplicate elements.
- If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
public void ForEach (Action action);
action: It is the Action<T> delegate to perform on each element of the List<T>.
- ArgumentNullException: If the action is null.
- InvalidOperationException: If an element in the collection has been modified.
Below programs illustrate the use List<T>.ForEach(Action<T>) Method:
Geeks For Geeks GFG C# Tutorials GeeksforGeeks
6 7 8 9 10 11 12