· c lambda foreach functional

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.

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:

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.

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

Is there?

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket