3 ms·
This isn’t a great example of what linq is good at. There’s no reason to do ToList there, and the ForEach isn’t particularly idiomatic
by klysm 2y ago
This isn’t a great example of what linq is good at. There’s no reason to do ToList there, and the ForEach isn’t particularly idiomatic
- pjc50 2y agoYeah, I hit the problem that there isn't a null-type equivalent of Select() for Action<T>, nor is there a IEnumerable.ForEach (controversial), so that's a bit of a hack. But I wanted to make it as close to the original example as possible.
- bob1029 2y ago> There’s no reason to do ToList there In this case, I would move it to the very end if we are concerned about the underlying data shifting when the collection is actually enumerated. Forgetting to materialize LINQ results can cause a lot of trouble, oftentimes in ways that happily evade detection while a debugger is attached.
- klysm 2y ago> if we are concerned about the underlying data shifting when the collection is actually enumerated I’m not sure what you mean by this. You can fulfill the IEnumerable contract without allowing multiple enumerations, but that doesn’t really have to do with the data shifting around. Doing ToList can be an expensive and unnecessary allocation
- DeathArrow 2y agoYes, ForEach isn't idiomatic but he could use Select instead.
- klysm 2y agoSide effects in a Select is not idiomatic either