C# Paradigms: Side effects on Lists

前端 未结 6 1711
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 11:11

    There is nothing wrong with it perse, except that you need to iterate it somehow, like calling Count() on it.

    From a 'style' perspective it is not good. One would not expect an iterator to mutate a list value/property.

    IMO the following would be better:

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

    The intent is much clearer to the reader or maintainer of the code.

提交回复
热议问题