Csharp lambda expression pros/cons


A noob csharp (or general programming) question.

Is there any real benefit on using lambda expression as an argument rather than using a loop?

I’m asking because personally I find using loop more easily readable.
Is the benefit only because you type less or is there something else?


Readability is very high prio. If only you is reading your code, I’d go for what is the most readable. But as usual, readability is very much about what you get used to… :slight_smile:

// Rolf

Well, I know that people have different preferences and to some lambda is better.

I was very surprised and astonished even to understand csharp’s lambda look like this => that’s crazy :smiley:

Lambdas can be useful if you only need a simple function once.

The drawback of lambdas is that they are anonymous functions, so during debugging callstacks can become difficult to understand.

If you are in a situation where you type the same code for lambdas in different places you should create just real functions: DRY principle (don’t repeat yourself).

1 Like