Mark Needham

Thoughts on Software Development

C#’s Lambda ForEach: Only on Lists?

with 8 comments

One of my favourite things introduced into C# recently is the new ForEach method which can be applied to (apparently only!) lists.

Last week we had a situation where we wanted to make use of the ForEach method on an IDictionary which we were using to store a collection of Selenium clients.

1
IDictionary<string, ISelenium> seleniumClients = new Dictionary<string, ISelenium>();

We wanted to write a piece of code to exit all of the clients when our tests had completed. We thought the following would do the trick:

1
seleniumClients.Values.ForEach(client => client.Stop());

The problem is that code doesn’t actually compile!

‘seleniumClients.Values’ returns an ICollection which extends IEnumerable so we thought ForEach should be available.

We eventually got around the problem by putting the collection into a list and then applying the ForEach method but it seems like there should be a better way to do this.

1
new List<ISelenium>(seleniumClients.Values).ForEach(client => client.Stop());

Is there?

Written by Mark Needham

December 15th, 2008 at 11:52 pm

Posted in .NET

Tagged with , , ,

  • http://blog.magenic.com/blogs/aarone Aaron Erickson

    Why not just write an extension method over IEnumerable for your app, like Microsoft apparently forgot to do :)

  • http://www.markhneedham.com Mark Needham

    Good point! Guess I’m still not quite in the mindset of getting the extension methods out there.

    I think this does the trick:

    public static class IEnumberableExtensions
    {
    public static void ForEach(this IEnumerable collection, Action action)
    {
    foreach (var item in collection)
    {
    action(item);
    }
    }
    }

  • http://iridescence.no Fredrik

    What’s wrong with

    foreach(var client in seleniumClients.Values) { client.Stop(); }

    ?

    Yeah, yeah I know – extension methods are cool :)

  • http://trgoodwin.blogspot.com Tim Goodwin

    Rather than reinventing this over and over, some people have started attempting to put this in a common code library.

    Check out:
    http://www.codeplex.com/nxl
    or
    http://www.codeplex.com/umbrella

    should be what you’re looking for.

  • Darshan

    Why don’t you use:
    seleniumClients.Values.ToList().ForEach(client => client.Stop());

  • Michael Schall

    Mark –

    We created a extension method almost identical to yours, but returned the collection so you could continue the call chain if you wanted.

    Mike

  • http://www.deanchalk.me.uk Dean Chalk

    how about

    seleniumClients.Values.ToList().ForEach(client => client.Stop());

  • Dan

    viewmodel.Lines.ToList().ForEach(f => { if (f.Line.RowState == Service.RowState.Modified) proxy.UpdateLineAsync(f.Line); });