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

Posted by Lernkurve on Stack Overflow See other posts from Stack Overflow or by Lernkurve
Published on 2010-04-15T16:18:11Z Indexed on 2010/04/15 16:23 UTC
Read the original article Hit count: 243

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, if you are stuck with a non-functional language, say C#, what techniques are there to avoid for-loops or to get rid of them by refactoring? With lambda expressions and LINQ perhaps? If so, how?

Questions

So the question boils down to:

  1. Why are for-loops bad? Or, in what context are for-loops to avoid and why?
  2. Can you provide C# code examples of how it looks before, i.e. with a loop, and afterwards without a loop?

© Stack Overflow or respective owner

Related posts about for-loop

Related posts about functional-programming