Please enable Javascript to correctly display the contents on Dot Net Tricks!

C Sharp Lamda Expression

  Author : Shailendra Chauhan
Updated On : 16 Jul 2014
Total Views : 106,043   
 

The concept of lamda expression was introduced in C# 3.0. Basically, Lamda expression is a more concise syntax of anonymous method. It is just a new way to write anonymous methods. At compile time all the lamda expressions are converted into anonymous methods according to lamda expression conversion rules.The left side of the lamda operator "=>" represents the arguments to the method and the right side is the method body.

Lambda expression Syntax

 (parameters) => expression-or-statement-block 

Types of Lamda expression

  1. Statement Lamda

    Statement lambda has a statement block on the right side of the lambda operator "=>".

     x => { return x * x; }; 
  2. Expression Lamda

    Expression lambda has only an expression (no return statement or curly braces), on the right side of the lambda operator "=>".

  3. x => x * x; // here x*x is expression 

Features of lamda expression

  1. Lambda expressions themselves do not have type. In fact, there is no concept of a lambda expression in the CLR.

     // ERROR: Operator '.' cannot be applied to
    // operand of type 'lambda expression'
    Type type = ((int x) => x).ToString(); 
  2. A lambda expression cannot be assigned to an implicitly typed local variable since the lambda expressions do not have type.

     // ERROR: Cannot assign lambda expression to an
    // implicitly typed local variable
    var thing = (x => x); 
  3. Jump statements (break, goto, continue) are not allowed within anonymous method/lambda expression. Similarly, you cannot jump into the lambda expression/ anonymous method from outside.

  4. Variables defined within a lambda expression are accessible only within the scope of the lambda expression body.

  5. Lambda expressions are used generally with the Func and Action delegates. our earlier expression can be written as follows:

     Func sqr = x => x * x; 

Anonymous method with Lambda and delegate

class Program
{
 //delegate for representing anonymous method
 delegate int del(int x, int y);

 static void Main(string[] args)
 {
 //anonymous method using expression lamda
 del d1 = (x, y) => x * y; 
 // or (int x, int y) => x * y;

 //anonymous method using statement lamda
 del d2 = (x, y) => { return x * y; }; 
 // or (int x, int y) => { return x * y; };

 //anonymous method using delegate keyword
 del d3 = delegate(int x, int y) { return x * y; };

 int z1 = d1(2, 3);
 int z2 = d2(3, 3);
 int z3 = d3(4, 3);

 Console.WriteLine(z1);
 Console.WriteLine(z2);
 Console.WriteLine(z3);
 }
}
//output:
6
9
12

Lamda expression as an Event Handler

 <form id="form1" runat="server">
 <div align="center">
<h2>Anonymous Method Example</h2>
 <br />
 <asp:Label ID="lblmsg" runat="server" ForeColor="Green" Font-Bold="true"></asp:Label>
 <br /><br />
 <asp:Button ID="btnReset" runat="server" Text="Reset" />  
 <asp:Button ID="btnSubmit" runat="server" Text="Submit" />  
 <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
 </div>
 </form> 
 protected void Page_Load(object sender, EventArgs e)
 {
 // Click Event handler using Regular method
 btnReset.Click += ClickEvent;
 // Click Event handler using Anonymous method
 btnSubmit.Click += delegate { lblmsg.Text="Submit Button clicked using Anonymous method"; }; 
// Click Event handler using Lamda expression 
btnCancel.Click += (senderobj, eventobj) => { lblmsg.Text = "Cancel Button clicked using Lamda expression"; };
 }
 protected void ClickEvent(object sender, EventArgs e)
 {
 lblmsg.Text="Reset Button clicked using Regular method";
 } 
Summary

In this article I try to expose lamda expression with simple example. I hope after reading this article you will be able to use lamda expression to write anonymous method in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+