What is Lambda expressions in C# programming?


Lambda expressions was added in .Net 3.5 framework. It is just like anonymous method. Lambda expressions is more flexible. Lambda expressions uses Lambda Operator i.e. =>. So in this article I will tell you about Lambda expressions with a sample C# code.

Learn What is Lambda expressions in C# programming


Lambda expressions is an anonymous method and introduced in .Net 3.5 framework. It uses =>, which is called Lambda Operator.

There are mainly two part of any Lambda expressions which is separated by Lambda Operator. Left side of Operator defines the input parameter and right side of operator defines statement or expression code.

Syntax:

input parameters => expression or statement.

Example:

1.a=>a*a
It is read "a goes to a times a".

2.(a,b)=>a=b

Sample code

delegate bool D1();
delegate bool D2(int i);

class Test1
{
D1 del1;
D2 del2;
public void Test(int in)
{
int a = 0;
del1 = () => { a = 10; return a > in; };


del2 = (x) => {return x == a; };

Console.WriteLine("a = {0}", a);
bool boolResult = del1();

Console.WriteLine("a = {0}. b = {1}", a, boolResult);
}

public static void Main()
{
Test1 test = new Test1();
test.Test(7);
bool result = test.del2(10);
Console.WriteLine(result);
Console.ReadKey();
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: