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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; namespace AnonymousMethodsTutorial { class Program { public delegate int SumTwoNumberDelegate(int numberOne, int numberTwo); static void Main(string[] args) { SumTwoNumberDelegate sumOfTwoNumber = delegate (int x, int y) { return x + y; }; int sum = sumOfTwoNumber(2, 2); Console.WriteLine(sum); } } } |
We can simplify this code by removing delegate keyword and inserting => operator between anonymous method’s parameters and statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; namespace AnonymousMethodsTutorial { class Program { public delegate int SumTwoNumberDelegate(int numberOne, int numberTwo); static void Main(string[] args) { SumTwoNumberDelegate sumOfTwoNumber = (int x, int y) => { return x + y; }; int sum = sumOfTwoNumber(2, 2); Console.WriteLine(sum); } } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; namespace AnonymousMethodsTutorial { class Program { //public delegate void SumTwoNumberDelegate(int numberOne, int numberTwo); static void Main(string[] args) { //SumTwoNumberDelegate sumOfTwoNumber = (int x, int y) => //{ // int sumTwoNumbers = x + y; // Console.WriteLine(sumTwoNumbers); //}; Action<int, int> sumOfTwoNumber = (int x, int y) => { int sumTwoNumbers = x + y; Console.WriteLine(sumTwoNumbers); }; sumOfTwoNumber(2, 2); } } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; namespace AnonymousMethodsTutorial { class Program { //public delegate int SumTwoNumberDelegate(int numberOne, int numberTwo); static void Main(string[] args) { //SumTwoNumberDelegate sumOfTwoNumber = (int x, int y) => { return x + y; }; Func<int, int, int> sumOfTwoNumber = (int x, int y) => { return x + y; }; int sum = sumOfTwoNumber(2, 2); Console.WriteLine(sum); } } } |
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.