tags:

views:

97

answers:

4

I am looking for a built-in extension method like Apply:

collection.Apply (predicate)

which will apply a method on all items contained in the collection.

I am asking first so that I don't write something that already exists.

EDIT: The reason I didn't use foreach was because I want to express the method using LINQ.

A: 

Am not sure if there is something like that but why don't you use foreach, since you are applying that method on all the items.

Issa Qandil
+3  A: 

Well, there's List<T>.ForEach, but nothing on IEnumerable<T>. It's the kind of thing that's in plenty of 3rd party "extra extension methods" toolkits though :)

I'd agree with the other answers that foreach is usually a better way to go - although it could occasionally make the difference between:

DoSomething(x => x.Where(...).Select(...).ForEach(...));

and

DoSomething(x => {
    foreach (var y in x.Where(...).Select(...)) {
        (...)
    }
});

which is more significant than just a single line.

I think I'd usually only use such a method if I already had a delegate to hand, or potentially a method group. For example, it's quite nice to be able to have a one-liner to dump a filtered collection:

people.Where(p => p.Age >= 18).ForEach(Console.WriteLine);

It's possible that after more exposure to functional programming I'd use it more often though.

Jon Skeet
Thanks Jon. Do you know any such toolkits for EMs? I checked the net but it shows more documentation than any toolkits/classes.
Joan Venge
Well, there's my own MoreLINQ project to start with: http://code.google.com/p/morelinq/
Jon Skeet
Thanks Jon, I am gonna yours :)
Joan Venge
+1  A: 

I'm pretty sure there isn't one for IEnumerable<T>, ICollection<T>, or IList<T>.

The List<T> class, however, has a generic ForEach method (built-in, not extension), which you can use as such:

list.ForEach(delegate);

Personally, I don't any real value to such methods. A simple foreach loop uses a single line more and offers the same usability, but there's nothing stopping you from writing one yourself of course.

Noldorin
+1  A: 

Well I for one can't live without the basic ForEach(...) extension method, maybe because I've swallowed the functional pill or something.

public static void ForEach<T>(this IEnumerable<T> sequence, Action<T> action)
{
    if (sequence == null) throw new ArgumentNullException("sequence");
    if (action == null) throw new ArgumentNullException("action");

    foreach (T t in sequence)
        action(t);
}

Some might say this just saves a couple of lines of code. But I think there is more to it than that: It saves a variable declaration and my personal favorite is that it plays really well with intellisense.

i.e.

people.Where(p => p.Age < 38)
      .Select(p => p.Firstname + " " + p.Surname)
      .ForEach(fullname => Console.WriteLine(fullname));

Is both much easier and quicker to write than the foreach loop because of the power of intellisense.

Never underestimate the importance of intellisense in API design.

Alex James
Thanks Alex for the code sample.
Joan Venge