May 182013
 May 18, 2013  Posted by at 11:39 am C# & F#, Not So Stupid Questions  Add comments

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

I had been banging my head against the wall (or maybe it would be more fitting to say computer screen) trying to find out what was wrong when I couldn’t find ForEach() on a list. I was working on portable class library targeting Windows Store Apps and Windows Phone 8. I don’t use it often, but that particular time I was looking for it. I asked on Twitter while I had a second look in the documentation.

A piece seems to be missing, should it have been there in the first place?

A piece seems to be missing, should it have been there in the first place?

I realized a few things.
a) Quite a few devs didn’t know it existed (nothing wrong with that- but made a few believe I didn’t know how to do a foreach 😀 )
b) It was indeed missing
c) The green suitcase shopping bag (thanks for poiting that out Laurent Bugnion – now I know why knows their shopping 😀 ) icon next to the member of a type means it is supported in Windows Store Apps, and the two books plus a windows means it is supported in portable class libraries.

AsReadOnly() and ConvertAll() were both not supported as well

Picture proof, ForEach is missing

Picture proof, ForEach is missing

The next question was obvious, why?

While I’ve tried to remember where I read this – unfortunately I can’t find it since this discussion on Twitter is a bit old and I can’t find all the tweets with the links, but when it was decided what to support language wise in Windows Store Apps decisions were made to remove what was considered unnecessary and shouldn’t have been there in the first place. ForEach() was one.
I was of course quite curious as to why. The answer I got was ‘number of potential problems ‘ Of course I wanted to know what those potential problems could be. That turned out to be harder to answer.

Seems like it is because:
A) The ‘good old foreach’ is more readable
B) Developers might be less tempted to change the collection while they are iterating through it and cause problems
C) If you really want it you can quite easy implement it yourself

Some sources:
Stackoverflow 1
Stackoverflow 2
Eric Lippert talks philosphy between foreach and ForEach()

My next question is, is it worth it- creating an extension method? Here is a part of that discussion

I’m keen on some more opinions on this, so if you have one – please share 🙂

  2 Responses to “Stupid Question 186 – 187: Is ForEach missing from List for Windows Store Apps? If so why?”

  1. Eric’s reasons are pretty compelling to me (I will confess to having a bias for functional).
    Something related that I discovered in JavaScript, and I suspect is true for C#, is that ForEach() is more expensive than foreach. Eric alludes to this when he mentions closures. I believe that ForEach() creates a new stack frame for each member. When working with large collections in JavaScript, this had a significant performance impact and I had to rewrite a lot of code to use old fashion for loops.

  2. IEnumerable.Select is another alternative without needing to create your own extension method. You can give it a dummy return value if necessary.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



What is 5 + 8 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)