Is the List.ForEach() method gone?

后端 未结 3 1527
孤街浪徒
孤街浪徒 2020-12-03 02:48

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 mor

相关标签:
3条回答
  • 2020-12-03 03:01

    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.

    0 讨论(0)
  • 2020-12-03 03:08

    An alternative is to define it yourself of course:

    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)

    0 讨论(0)
  • 2020-12-03 03:16

    It's indeed gone:

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


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

    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.

    0 讨论(0)
提交回复
热议问题