lambda expressions in C#
lambda expression
suppose I have a scenario like I want to add a couple of numbers like into a is
equal to C plus D we are C and B are some separate variables.
just having some
values inside alright and let's compare this particular statement or expression
with the method which is returning an integer called sum and inside this again
we are doing the very same scenario like return C plus D all right so let me
compare the functionality or the performance I should say between these two in
this first case.
what is there C and D
would be having their value they will be summed up and the result will go to a
while here if I will have to call this sum so what will happen first of all
this sum note sum method will be loaded in the call stack then maybe if there
are parameters the parameters will be copied the sum the summing will be done
and then the result will be returned and after all these things the call stack
will be removing means this matter will be popped out from the call stack so
this loading and unloading will be consuming some time as well and we can also
notice like this particular thing is not that big you can even do this in a
single line rather than calling a method .
when the
functionality is very small you can easily replace a method with an expression
for a better functionality so here we are going to do the similar thing with lambda expressions as well as in the we
have noticed like we bound an anonymous method directly with a but if I'm not
going to do a bigger task.
I can anytime replace that method Anonymous or any
particular method with the expression called lambda expression so let's find it
practically how can we do that for going through the implementation of lambda
expressions .
using System;
public delegate void MessageDelegate(string message);
public class Program
{
public static void Main()
{
MessageDelegate ShowMessage = new MessageDelegate(
delegate(string message)
{
Console.WriteLine(message);
}
);
ShowMessage("Hello World!");
}
}
By using lambda expressions, we can simplify the code like this:
using System; public delegate void MessageDelegate(string message); public class Program { public static void Main() { MessageDelegate ShowMessage = (message) => Console.WriteLine(message); ShowMessage("Hello World!"); } }
First, we indicate the parameter(s) without any type, then we use the => operator. We then write the statement(s) to be executed. The type of the parameter will automatically be detected by the compiler. Of course the lambda expression must still match the signiture of the delegate. If your anonymous method needs multiple statements, then you simply put braces.
MessageDelegate ShowMessage = (message) => { Console.WriteLine(message); Console.WriteLine("Some more message"); } Using lambda expressions, it is now easier to pass anonymous methods as arguments to other methods. Consider the example below. using System; public delegate int Arithmetic(int x, int y); public class Program { public static int GetResult(Arithmetic Operation) { return Operation(20, 5); } public static void Main() { Console.WriteLine("20 + 5 = " + GetResult((x, y) => { return x + y; })); Console.WriteLine("20 - 5 = " + GetResult((x, y) => { return x - y; })); Console.WriteLine("20 * 5 = " + GetResult((x, y) => { return x * y; })); Console.WriteLine("20 / 5 = " + GetResult((x, y) => { return x / y; })); } } Figure 1 20 + 5 = 25 20 - 5 = 15 20 * 5 = 100 20 / 5 = 4
MessageDelegate ShowMessage = (string message) => Console.WriteLine(message);
If you indicate the type of one parameter, then the other parameters should also have their type. For example, you cannot do the following. MessageDelegate ShowMessage = (string message1, message2) => Console.WriteLine(message1);
If you delegate for your lambda expression has no parameters, then all you need to do is don't provide any parameters in the lambda expression. MessageDelegate ShowMessage = () => Console.WriteLine("Hello");
Post A Comment:
0 comments: