C# Paradigms: Side effects on Lists

前端 未结 6 1733
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 10:43

I am trying to evolve my understanding of side effects and how they should be controlled and applied.

In the following List of flights, I want to set a property of e

6条回答
  •  长情又很酷
    2020-12-29 11:11

    I like using foreach when I'm actually changing something. Something like

    foreach (var flight in fResults.Where(f => f.NonStop))
    {
      flight.Description = "Fly Direct!";
    }
    

    and so does Eric Lippert in his article about why LINQ does not have a ForEach helper method.

    But we can go a bit deeper here. I am philosophically opposed to providing such a method, for two reasons.

    The first reason is that doing so violates the functional programming principles that all the other sequence operators are based upon. Clearly the sole purpose of a call to this method is to cause side effects.

提交回复
热议问题