IEnumerable.ForEach(...)
An extension method to look over a sequence of elements and perform an action on each element.
void ForEach<T>(this IEnumerable<T> sequence, Action<T, int> action)
Parameters
sequence
: Extends theIEnumerable<T>
interface.action
: An action to perform on each element in thesequence
. It takes two parametersT
: A single element from the sequence.int
: The (virtual) index of the element in the sequence.
Example
aSequence.ForEach((e) => {
Console.WriteLine($"Item: {e}");
});
void ForEach<T>(this IEnumerable<T> sequence, Action<T, int> action)
Parameters
sequence
: Extends theIEnumerable<T>
interface.action
: An action to perform on each element in thesequence
. It takes a single parameterT
: A single element from the sequence.
Example
aSequence.ForEach((e,i) => {
Console.WriteLine($"Item {i}: {e}");
});