I started dabbling in Windows 8 metro recently, and found that one of my old buddies seems to have gone missing.
I tend to use the .ForEach()
method more than I use the traditional foreach()
construct, and I realized pretty quickly that this method isn't available. For example, this code will not compile under a metro app:
var list = new List<string>();
list.ForEach(System.Diagnostics.Debug.WriteLine);
I've searched to see if I could find any discussion of this, but wasn't able to. Am I just being obtuse, or is it actually gone?
It's indeed gone:
Very strangely, however, it makes an appearance in the documentation, in which nowhere does it state that this method isn't supported in .NET for Windows Store apps (formerly .NET for Metro-style apps). Perhaps this is just an oversight on part of the documentation team.
To get a sense for why it might no longer be included, read this post by someone who works on the C# team at Microsoft: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx
Basically, it's philosophy. The "LINQ" features are highly inspired by the functional programming paradigm, and the ForEach extension flies in the face of that... it encourages poor functional style.
An alternative is to define it yourself of course:
Credit: LINQ equivalent of foreach for IEnumerable<T>
(Note: not a duplicate)