oliverbooth / X10D

Extension methods on crack.
https://oliverbooth.github.io/X10D/
MIT License
28 stars 2 forks source link

IEnumerable.For and IEnumerable.ForEach #50

Closed oliverbooth closed 2 years ago

oliverbooth commented 2 years ago

Type

System.IEnumerable<T>

Extension method signature

void For<T>(System.Action<int, T>);
// and
void ForEach<T>(System.Action<T>);
Parameters For Parameter Type Description
action System.Action<int, T> The Action<int, T> delegate to perform on each element of the IEnumerable<T>. The int argument passed to this delegate represents the index.
ForEach Parameter Type Description
action System.Action<T> The Action<T> delegate to perform on each element of the `IEnumerable.

Description Performs the specified action on each element of the IEnumerable<T>.

Benefits Similar functionality exists built-in for List<T>, see https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach?view=net-6.0 - this functionality is not provided for IEnumerable<T>

Drawbacks Potential naming collisions when attempting to call on a List<T>

(Optional) Implementation example

{
    foreach (T item in source)
    {
        action(item);
    }
}