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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Justin Skiles
  • 9,373
  • 6
  • 50
  • 61

1 Answers1

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It appears to have more to do with problems that come with mutating the list within the callback: http://stackoverflow.com/questions/10299458/is-the-listt-foreach-extension-method-gone/10299492#10299492 – BoltClock Nov 03 '12 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 Nov 03 '12 at 18:40
  • Agreed about it adding unnecessary bloat, though. I never really saw the point. – BoltClock Nov 03 '12 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 Dec 22 '12 at 02:52
  • 1
    `foo.thing?.forEach { ... }` is the prime reason it should be included. `foreach(var thing in foo.thing ?? new List())` 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 Jun 25 '18 at 19:42