How to replace for-loops with a functional statement in C#?

前端 未结 18 1752
不思量自难忘°
不思量自难忘° 2021-01-31 08:59

A colleague once said that God is killing a kitten every time I write a for-loop.

When asked how to avoid for-loops, his answer was to use a functional language. However

18条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 09:23

    The question is if the loop will be mutating state or causing side effects. If so, use a foreach loop. If not, consider using LINQ or other functional constructs.

    See "foreach" vs "ForEach" on Eric Lippert's Blog.

提交回复
热议问题