Navigate to topics
Lambdas & Delegates
Lambdas are popularly known as “Arrow Functions” or “Lambda
Expressions”. Lambdas are way to create delegates or pointer to functions in
the shortest possible way. Lambdas are language agnostic, hence can
interoperate across languages. Lambdas can use language specific constructs,
while the developer can write lambdas in a language agnostic manner as well.
Lambda expression is a short form of creating function
pointers or delegates. In this example, we shall take an example in C#.Net.
Let’s take an example.
A traditional delegate is declared in the following way in
c#.
delegate int Compute(int a, int b);
Every delegate has 3 stages in its lifecycle.
1.
Delegate Declaration
2.
Delegate Instantiation
3.
Delegate Invocation
Here are the three stages of the delegate.
1. using System;
2.
3. namespace Demo
4. {
5. //Declaration
6. delegate int Compute(int a, int b);
7. class WorkWithDelegates
8. {
9.
10. private int Add(int a, int b)
11. {
12. return a + b;
13. }
14.
15. void Start()
16. {
17. //Instantiation. Viz Point Compute Delegate to a Function Add
18. //Uses a named function Add
19. Compute objCompute = Add;
20. //Invoke the delegate
21. objCompute(10, 20);
22. objCompute(100, 200);
23. }
24. }
25. }
Video Tutorial (Concept & Support Engineer Example)
Anonymous Functions
The above code can be shortened in the following manner. Look at line 18 in
the code below. Here we make use of anonymous functions. Anonymous functions
are un-named functions which are declared and implemented in the same line of
code.
1. namespace Demo
2. {
3. delegate int Compute(int a, int b);
4. class WorkWithDelegates
5. {
6.
7.
8. private int Add(int a, int b)
9. {
10. return a + b;
11. }
12.
13. void Start()
14. {
15. //Uses a named function Add
16. Compute objCompute = Add;
17. //Does not use Add(). Anonymous function is created
18. Compute objNewCompute = delegate (int a, int b) { return a + b; };
19.
20. objCompute(10, 20);
21. objCompute(100, 200);
22.
23. }
24. }
25. }
26.
In the above code, the named function Add is replaced with
anonymous function using the keyword delegate()
Delegates using Lambda Syntax
With the introduction of Lambdas, these three steps can be done
in a much easier way, with application to creating delegates. Lambda functions
are known as Arrow functions. Lambdas are used by every modern language
including javascript and its variants.
The basic syntax of a Lambda expression is
RHS => LHS
“=>”
is the Lambda operator which is read as “Goes To”. With Lambdas, the above code
can be modified slightly in a language independent way. This means, that the c#
language constructs will not be used. Here we are making our code language-agnostic.
This also means that it is not required to use the C# syntaxes of {}, return,
; in the line 18 in
the above code. To view the final changes, have a look at the code block below.
1. delegate int Compute(int a, int b);
2. //Uses a named function Add
3. Compute objCompute = Add;
4. //Does not use Add(). Anonymous function is created
5. Compute objNewCompute = delegate (int a, int b) { return a + b; };
6. //Using Lambdas
7. Compute objLambdaCompute = (a, b) => a + b;
8. //Invoke the lambda
9. objLambdaCompute(10,20)
10.
But if you observe carefully, we still have to declare the
compute delegate at line 1, in the above code block. To solve this step, C# has
introduced Generic delegates.
Lambda Expressions (Video Tutorial)
Generic Delegates using Lambdas
The three-part process of “Declaration of a named
Delegate – Instantiation – Invocation” would be reduced to just two lines
of code. C# introduces three types of generic delegates.
1.
Func<>: Known as Function Delegate. A
delegate that points to a method that
1.
Can accept any number of parameters
2.
And returns any type except void.
2.
Action<>: Known as Action Delegate. A delegate
that points to a method that
1.
Accepts any number of parameters
2.
And ONLY returns void
3.
Predicate<>: Known as Predicate Delegate.
A delegate that points to a method that
1.
Accepts an input parameter
2.
And ONLY returns Boolean
Structure
Usage
1. void UseLambdaDelegates()
2. {
3. Func<int, int, int> Compute = (a, b) => a + b;
4. Console.WriteLine($"Value of Func<> is: {Compute(10, 20)}"); //Output: Value of Func<> is: 30
5.
6. Action<string> Greet = (msg) => Console.WriteLine(msg);
7. Greet("Namaste, Welcome to Inspire Innovative Learning!"); //Output: Namaste, Welcome to Inspire Innovative Learning
8.
9. Predicate<string> ContainsNamaste = (str1) => str1.ToLower().Contains("namaste");
10. Console.WriteLine(ContainsNamaste("Namaste, Welcome to Inspire Innovative Learning!")); //Output: True
11. }
12.
Hope you enjoyed this. If you wish to learn more, do enroll
for courses on .Net technologies – Desktop – Web – UWP – Azure by getting in
touch with me in the comments.
No comments:
Post a Comment