List.ForEach interface and collections

In .NET 3.5, List <> gets the ForEach method. I notice that this does not exist on IList <> or IEnumerable <> what was here here? Is there any other way to do this? A beautiful and easy way to do this?

I ask because I was in a conversation where the speaker always used more general interfaces. But why should I use IList <> as the return type if I want to be able to deploy and use ForEach? Then I just put it back in List <>.

+3
source share
3 answers

what was here here?

blog , .

? ?

foreach? .

foreach (var foo in ilist)
{
    // etc...
}

ForEach IEnumerable<T>, :

public static void ForEach<T>(this IEnumerable<T> enumeration, Action<T> action)
{
    foreach (T item in enumeration)
    {
        action(item);
    }
}

.

+4

(), Rx .Run( IEnumerable ), , List custom ForEach. , .

+1

Why is ForEachnot in IEnumerable<T>? Eric Lippert explains this beautifully on his blog . In principle, LINQ should be functional (no side effects), but ForEachclearly non-functional.

But why is it not in IList<T>? Well ... it should be !

+1
source

Source: https://habr.com/ru/post/1748247/


All Articles