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