Mark Needham

Thoughts on Software Development

C#'s Lambda ForEach: Only on Lists?

with 6 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?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • StumbleUpon
  • Twitter

Written by Mark Needham

December 15th, 2008 at 11:52 pm

Posted in .NET

Tagged with , , ,

6 Responses to 'C#'s Lambda ForEach: Only on Lists?'

Subscribe to comments with RSS or TrackBack to 'C#'s Lambda ForEach: Only on Lists?'.

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

    Aaron Erickson

    16 Dec 08 at 12:29 am

  2. 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);
    }
    }
    }

    Mark Needham

    16 Dec 08 at 1:03 am

  3. What's wrong with

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

    ?

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

    Fredrik

    16 Dec 08 at 1:48 am

  4. 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.

    Tim Goodwin

    16 Dec 08 at 6:32 am

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

    Darshan

    16 Dec 08 at 4:08 pm

  6. 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

    Michael Schall

    17 Dec 08 at 12:42 am

Leave a Reply