7

I was porting some code over to work with a Windows Store App and noticed that the List<T>.ForEach method isn't included in the .NET Core framework (the framework referenced by Windows Store Apps).

MSDN confirms that it is not supported for Store Apps.

I can easily get around the missing method, but at this point I'm just curious why it's missing.

2

1 Answer 1

16

I can easily get around the missing method, but at this point I'm just curious why it's missing.

I suspect it was deemed unnecessary bloat which arguably would have been better not in the framework to start with, as using foreach is usually cleaner.

Indeed, from the link at the bottom of that blog post, MSFT provides an answer in the MSDN forums:

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.

5
  • It appears to have more to do with problems that come with mutating the list within the callback: stackoverflow.com/questions/10299458/…
    – BoltClock
    Commented Nov 3, 2012 at 18:39
  • 1
    @BoltClock: Was just linking to the same original source. I'm not sure that I buy mutation being more of a problem in ForEach than foreach though - surely it can fail in the same way in both cases.
    – Jon Skeet
    Commented Nov 3, 2012 at 18:40
  • Agreed about it adding unnecessary bloat, though. I never really saw the point.
    – BoltClock
    Commented Nov 3, 2012 at 18:44
  • Perhaps the reasoning was, if you're using foreach, it feels like you're not allowed to mutate the collection, but in a ForEach callback, developers might not feel the same way.
    – doug65536
    Commented Dec 22, 2012 at 2:52
  • 1
    foo.thing?.forEach { ... } is the prime reason it should be included. foreach(var thing in foo.thing ?? new List<Thing>()) is painful and requires extra unnecessary allocation of a List so you end up having to do an if check outside of the loop.
    – Benhamine
    Commented Jun 25, 2018 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.