Discussing Lambda Expression in C#

A Lambda expression is nothing but an Anonymous Function, can contain expressions and statements.
 
Lambda expressions can be used mostly to create delegates or expression tree types.
 
Lambda expression uses lambda operator => and read as ‘goes to’ operator.
 
Left side of this operator specifies the input parameters and contains the expression or statement block at the right side.
 
Check the following example
  1. myExp = myExp/10;  
Now, let see how we can assign the above to a delegate and create an expression tree
  1. delegate int myDel(int intMyNum);    
  1. static void Main(string[] args)    
  2. {    
  3.    //assign lambda expression to a delegate:    
  4.    myDel myDelegate = myExp => myExp / 10;    
  5.    int intRes = myDelegate(110);    
  6.    Console.WriteLine(”Output {0}”, intRes);    
  7.    Console.ReadLine();    
  8.    //Create an expression tree type    
  9.    //This needs System.Linq.Expressions    
  10.    Expression myExpDel = myExp => myExp /10;    
  11. }  
Please note that:
  • The => operator has the same precedence as assignment (=) and is right-associative.
  • Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as Where.
Next Recommended Reading Lambda Expression In C#