TagAnonymous methods

Lambda expressions

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 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 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.


Anonymous methods

Anonymous methods allow us to create delegate methods inline.

What does that mean?

Let’s create a simple delegate whose role is to sum two numbers:

Would not it be nice if we could get rid of the SumTwoNumber method and just write it’s content while we instantiate delegate ? Yes it would and we can do it! Steps are:

  1. While instantiating your delegate, remove new… part.
  2. Write delegate keyword followed by input parameter in round brackets.
  3. Type your code inside curly brackets.

What we essentially did is we created a method without method name and thus we created anonymous method. Don’t believe me? Look again at the same code that has been reformatted a bit:

When you mentioned we can write delegate methods inline, does that mean we can write methods in one line only?

No, you can write as many lines as you want. Notice semicolon (;) on line 12, you can write additional code below. But beware of that feature, because if you write too many lines whole purpose of anonymous methods disappears and that is readability. Generally you would like to keep it to 1-3 lines, more than that would best be very good candidate for standalone method.

How do i know which parameters and return types to use wile writing an anonymous methods?

Your delegate tells you that information. In our example SumTwoNumberDelegate delegate expects return type int and two int input parameters, so that is what we provided him within anonymous method.

Anonymous methods are awesome! Are there any drawbacks to using them?

You would normally use delegate, instead of method, if you do not intend to use a function multiple times. On the other hand it makes no sense to write same anonymous method multiple times if you can reuse one method + it is easier to maintain that one method than multiple anonymous methods.

Can i make my code even simpler?

Yes you can, by using lambda expressions!


© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑