A lambda expressions are an anonymous function that helps us write easy to read code.

Let’s see how we can achieve this. First we will take example from previous post about anonymous methods:

We can simplify this code by¬† removing delegate keyword and inserting => operator between anonymous method’s parameters and statement.

How do you pronounce => operator?

There are quite a few names for => operator. Here are a few typical names:

  • Goes to
  • Maps
  • Such that
  • Becomes

Lambda expressions do not look like a easier way to write delegates, can we for example remove delegate declaration?

You are correct. We can simplify thing even more with Action and Func.

Action

Action is a generic delegate type that describes a method that does not return a value. Action can have 16 overloads, of which not a single one describes a return type. You can picture action like a method with a void return type.

Action example

Code here is not deleted but commented out for you to see benefits of using Action. First of we do not need to declare delegate and lastly because we do not need to declare delegate we are free to decide upon our types while writing an Action.

Func

Func is a generic delegate type that describes a method that has an return a value. Func can have 17 overloads, but note that last type is your return type.

Func Example

Same as Action but note, that when looking at our sumOfTwoNumber Func, third int type is our return type.

We will now continue our journey with delegates by learning about events.