LambdaExpressions.md - brainchildservices/curriculum GitHub Wiki
SLIDE-1
Lambda expression is a shorter way of representing anonymous methods.
The difference that in Lambda expressions you don’t need to specify the type of the value that you input thus making it more flexible to use.
The ‘=>’ is the lambda operator which is used in all lambda expressions.
The Lambda expression is divided into two parts, the left side is the input and the right is the expression.
A lambda expression can be of any of the following two forms:
- Expression lambda: Expression lambda that has an expression as its body
- Statement lambda: Statement lambda that has a statement block as its body:
SLIDE-2
Example1:
- Method call using delegate:(C# 1)
https://dotnetfiddle.net/OqhxwX
using System;
public delegate string PrintDelegate(string name); //2. Create the Delegate
public class Program
{
public static void Main()
{
PrintDelegate printDel = new PrintDelegate(Print); //3. Create instance of Delegate
string result = printDel("John"); //4. Invoke the Delegate
Console.WriteLine(result); //5. Print the result
}
public static string Print(string name) //1. Create the method
{
return "Hello " + name + ". Good Morning";
}
}
SLIDE-2(DOWNWARDS)
- Using anonymous method:(C# 2)
https://dotnetfiddle.net/AMKdi1
using System;
public delegate string PrintDelegate(string name); //1. Create the Delegate
public class Program
{
public static void Main()
{
PrintDelegate printDel = delegate(string name)
{
return "Hello " + name + ". Good Morning";
}; //2. Create instance of Delegate using delegate keyword and adding the method bodu
string result = printDel("John"); //3. Invoke the Delegate
Console.WriteLine(result); //4. Print the result
}
/*public static string Print(string name) //1. Create the method - No longer needed
{
return "Hello " + name + ". Good Morning";
}*/
}
SLIDE-2(DOWNWARDS)
- Using Lambda expression:(C# 3)
-
There is no need of the delegate keyword.
-
It is not necessary to explicitly specify the type of the parameter. The compiler can automatically enforce it at runtime
-
The parameters are followed by the lambda operator =>
https://dotnetfiddle.net/bo0E3uusing System; public delegate string PrintDelegate(string name); //1. Create the Delegate public class Program { public static void Main() { //*******The only change happens in step 2******* PrintDelegate printDel = (string name) => //2. Remove the delegate keyword and put the lambda operator after the parameter { return "Hello " + name + ". Good Morning"; }; string result = printDel("John"); //3. Invoke the Delegate Console.WriteLine(result); //4. Print the result } /*public static string Print(string name) //1. Create the method - No longer needed { return "Hello " + name + ". Good Morning"; }*/ }
SLIDE-3
Example2:
-
Without using anonymous method
https://dotnetfiddle.net/bzNROkusing System; public delegate void SumDelegate(int a, int b); //Step2: Created Delegate public class Program { public static void Main() { SumDelegate sumDel = new SumDelegate(Sum); //Step3: Created instance of delegate sumDel(2, 3); //Step4: Invoke the delegate } public static void Sum(int a, int b) //Step1: Create method { Console.WriteLine("{0}+{1}={2}", a, b, a + b); } }
SLIDE-3(DOWNWARDS)
-
Using anonymous method
https://dotnetfiddle.net/c0gxB9using System; public delegate void SumDelegate(int a, int b); //Step1: Created Delegate public class Program { public static void Main() { SumDelegate sumDel = delegate (int a, int b) //Step2: Created instance of delegate and pass method { Console.WriteLine("{0}+{1}={2}", a, b, a + b); }; sumDel(2, 3); //Step3: Invoke the delegate } }
SLIDE-3(DOWNWARDS)
-
Using lambda expression
https://dotnetfiddle.net/zNQv1Eusing System; public delegate void SumDelegate(int a, int b); //Step1: Created Delegate public class Program { public static void Main() { SumDelegate sumDel = (int a, int b) => //Step2: Create a lambda expression { Console.WriteLine("{0}+{1}={2}", a, b, a + b); }; sumDel(2, 3); //Step3: Invoke the delegate } }
SLIDE-4
Lambda expression makes the code look smaller. We don't have to specify the input parameter type, the return type, access modifier etc. Compiler takes care of all that.
A Lambda expression must be assigned to a delegate.
This can be done in one of two ways. We can use one of the built-in .NET delegates or provide our own local method.
-
Make a custom delegate and assigning the Lambda expression to it
https://dotnetfiddle.net/TPPsKcusing System; public delegate int RectDelegate(int length, int breadth); public class Program { public static void Main() { RectDelegate rectDel = (length, breadth) => { return length * breadth; }; int result = rectDel(2, 8); Console.WriteLine("Are of the rectangle is: " + result); } }
SLIDE-5
- Using Func generic delegate:
Func is a generic delegate. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter.
Examples:
- Func<int, int, int> : Means the method has 2 integer input parameters and int return type
- Func<string, string, int> : Means the method has 2 string input parameters and int return type
- Func<int, long, string> : Means the method has one int and one long input parameters and string return type
- Func<int, int> : Means the method has one int and int return type
- Func : Means the method has no input parameters and int return type
https://dotnetfiddle.net/v2iPy9
using System;
//public delegate int RectDelegate(int length, int breadth); //Custom delegate no longer needed as we are using Func
public class Program
{
public static void Main()
{
Func<int, int, int> rectDel = (length, breadth) => //Custom delegate replaced by Generic delegate Func
{
return length * breadth;
};
int result = rectDel(2, 8);
Console.WriteLine("Are of the rectangle is: " + result);
}
}
SLIDE-6
- Using Action generic delegate:
An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type.
- Action<int, int> : Means the method has 2 integer input parameters and void return type
- Action<string, string> : Means the method has 2 string input parameters and void return type
- Action<int, long> : Means the method has one int and one long input parameters and void return type
- Action : Means the method has one int and void return type
- Action<> : Means the method has no input parameters and void return type
https://dotnetfiddle.net/TpQFI8
using System;
//public delegate int RectDelegate(int length, int breadth); //Custom delegate no longer needed as we are using Action
public class Program
{
public static void Main()
{
Action<int, int> rectDel = (length, breadth) => //Custom delegate replaced by Generic delegate Func
{ //Changed the method into one that doesn't return a value
Console.WriteLine("Area of the rectangle is: " + (length * breadth));
};
rectDel(2, 8);
}
}
SLIDE-7
- Local delegate method:
In this case, there is no need to declare a custom delegate. But we would need to specify the return type and type of the parameter inputs.
It is similar to creating a method, except that there are no curly braces and the start of curly braces is replaced by => operator. And no return
Use .NET5 on dotnet fiddle.
Example 1:With return type
https://dotnetfiddle.net/CBncXz
using System;
// public delegate int RectDelegate(int length, int breadth); //Step1: No need of custom delegate
public class Program
{
public static void Main()
{
int rectArea(int length, int breadth) => (length * breadth); // Specify return type, remove = sign, add parameter type, no return keyword
// Expression on the right side of => is evaludated and returned
int result = rectArea(2, 8);
Console.WriteLine("Are of the rectangle is: " + result);
}
}
Example 1:With void return type
https://dotnetfiddle.net/wyoByN
using System;
public class Program
{
public static void Main()
{
void rectArea(int length, int breadth) => Console.WriteLine("Area of the rectangle is: " + length * breadth);
rectArea(2, 8);
}
}
SLIDE-8
Exercise:
- Write a instance(non-static) function that takes 3 input parameters and returns the product of it.
Create
(i) Call the method directly in the Main method
(ii) Call the method using custom delegate in Main method
(iii) Use the method anonymously in Main Method
(iv) Use custom delegate and assign to lambda expression
(v) Use Func delegate and assign to lambda expression
(vi) Use local delegate
SLIDE-9
- Write a static function Square which takes one parameter and returns the square of the parameter
Create
(i) Call the method directly in the Main method (ii) Call the method using custom delegate in Main method (iii) Use the method anonymously in Main Method (iv) Use custom delegate and assign to lambda expression (v) Use Func delegate and assign to lambda expression (vi) Use local delegate
SLIDE-10
- Write an instance function Person which takes two parameter, one string(name) and other int(age). The function returns void Create
(i) Call the method directly in the Main method (ii) Call the method using custom delegate in Main method (iii) Use the method anonymously in Main Method (iv) Use custom delegate and assign to lambda expression (v) Use Action delegate and assign to lambda expression (vi) Use local delegate
SLIDE-11
- Write an instance function ToUpper which takes 1 string parameter. The function prints the string in UpperCase. The function returns void Create
(i) Call the method directly in the Main method (ii) Call the method using custom delegate in Main method (iii) Use the method anonymously in Main Method (iv) Use custom delegate and assign to lambda expression (v) Use Action delegate and assign to lambda expression (vi) Use local delegate
References:
https://www.youtube.com/watch?v=KHNx349xZ1o
https://www.geeksforgeeks.org/lambda-expressions-in-c-sharp/
https://itnext.io/delegates-anonymous-methods-and-lambda-expressions-5ea4e56bbd05
https://www.koderhq.com/tutorial/csharp/lambda/
https://www.tutorialsteacher.com/csharp/csharp-func-delegate
https://www.tutorialsteacher.com/csharp/csharp-action-delegate