C#'s Lambda ForEach: Only on Lists?
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?
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
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(this IEnumerable collection, Action action)
{
public static void ForEach
{
foreach (var item in collection)
{
action(item);
}
}
}
Mark Needham
16 Dec 08 at 1:03 am
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
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
Why don't you use:
seleniumClients.Values.ToList().ForEach(client => client.Stop());
Darshan
16 Dec 08 at 4:08 pm
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