What Is The Lambda In C#
Introduction
The lambda is an unnamed function and it is generally used for creating delegates in LINQ. It is easily manipulate without a declaration of method that is return value, declaration or access modifier is called Lambda. There is question arise in our mind that why we required Lambda answer is that because it allow to create short write method on the same place where were you used a method only once. And where definition of method is very short. It reduced your time to write method to contain class.
The lambda is an unnamed function and it is generally used for creating delegates in LINQ. It is easily manipulate without a declaration of method that is return value, declaration or access modifier is called Lambda. There is question arise in our mind that why we required Lambda answer is that because it allow to create short write method on the same place where were you used a method only once. And where definition of method is very short. It reduced your time to write method to contain class.
A lambda expression make program easier, lambda expression is a anonymous function by creating delegates. we can pass local function by the value of the function calls.
Advantages of method
1. If you somewhere look method definition you don't required to read the code.
2. It reduced your time on typing the program.
2. It reduced your time on typing the program.
Sign of the Lambda
In the programming languages we have use sign(=>) of Lambda.
Generally we defined Lambda as
Parameter=>Execution code
In the programming languages we have use sign(=>) of Lambda.
Generally we defined Lambda as
Parameter=>Execution code
Example of the Lambda.
m=>n%2==1
m is the input parameter
m%2==1 is the expression
m is the input parameter
m%2==1 is the expression
Example
namespace ConsoleApplication
{
delegate bool D();
delegate bool D2(int i);
class Test
{
D del;
D2 del2;
public void TestMethod(int input)
{
int j = 0;
del = () => { j = 10; return j > input; };
del2 = (x) => { return x == j; };
Console.WriteLine("j = {0}", j);
bool boolResult = del();
Console.WriteLine("j = {0}. b = {1}", j, boolResult);
}
static void Main()
{
Test test = new Test();
test.TestMethod(5);
bool result = test.del2(10);
Console.WriteLine(result);
Console.ReadKey();
}
}
}
{
delegate bool D();
delegate bool D2(int i);
class Test
{
D del;
D2 del2;
public void TestMethod(int input)
{
int j = 0;
del = () => { j = 10; return j > input; };
del2 = (x) => { return x == j; };
Console.WriteLine("j = {0}", j);
bool boolResult = del();
Console.WriteLine("j = {0}. b = {1}", j, boolResult);
}
static void Main()
{
Test test = new Test();
test.TestMethod(5);
bool result = test.del2(10);
Console.WriteLine(result);
Console.ReadKey();
}
}
}
Output
j=0
j=10, b=True
True
No comments:
Post a Comment