Is the List <T> .ForEach () method gone?

Recently, I started chatting in Windows 8, and found that one of my old buddies seemed to be missing.

I prefer using the .ForEach() method more than the traditional foreach() construct, and I quickly realized that this method is not available. For example, this code will not compile in the metro application:

 var list = new List<string>(); list.ForEach(System.Diagnostics.Debug.WriteLine); 

I searched if I could find any discussion of this issue, but could not. Am I just dumb, or is he really gone?

+44
c # windows-8 windows-store-apps
Apr 24 2018-12-12T00:
source share
3 answers

It really passed:

The <T> .ForEach list has been removed in Metro style apps. Although the method seems simple, it has a number of potential problems when a list gets mutated by the method passed to ForEach. Instead, it is recommended that you simply use the foreach loop.




Wes Haggard | .NET Framework Team (BCL) | http://blogs.msdn.com/b/bclteam/

Very strange, however, it appears in the documentation, which does not say anywhere that this method is not supported in .NET for Windows Store applications (previously .NET for Metro-style applications). Perhaps this is just the supervision of the documentation team.

+42
Apr 24 2018-12-12T00:
source share

To understand why it can no longer be enabled, read this post to those who work in the C # team at Microsoft: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs -foreach.aspx

In principle, this is philosophy. The LINQ features are very inspired by the functional programming paradigm, and the ForEach extension flies in the face of this ... it encourages a poor functional style.

+18
Apr 24 2018-12-12T00:
source share

An alternative is to define this yourself:

 public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumeration, Action<T> action) { foreach(T item in enumeration) { action(item); yield return item; } } 

Credit: LINQ equivalent of foreach for IEnumerable <T>

(Note: not a duplicate)

+16
Apr 24 '12 at 14:00
source share



All Articles