Saturday, February 27, 2016

C Sharp Lamda Expression

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

  1. (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 "=>".
    1. 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 "=>".
    1. 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.
    1. // ERROR: Operator '.' cannot be applied to
    2. // operand of type 'lambda expression'
    3. 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.
    1. // ERROR: Cannot assign lambda expression to an
    2. // implicitly typed local variable
    3. 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:
    1. Func sqr = x => x * x;

Anonymous method with Lambda and delegate

  1. class Program
  2. {
  3. //delegate for representing anonymous method
  4. delegate int del(int x, int y);
  5.  
  6. static void Main(string[] args)
  7. {
  8. //anonymous method using expression lamda
  9. del d1 = (x, y) => x * y;
  10. // or (int x, int y) => x * y;
  11.  
  12. //anonymous method using statement lamda
  13. del d2 = (x, y) => { return x * y; };
  14. // or (int x, int y) => { return x * y; };
  15.  
  16. //anonymous method using delegate keyword
  17. del d3 = delegate(int x, int y) { return x * y; };
  18.  
  19. int z1 = d1(2, 3);
  20. int z2 = d2(3, 3);
  21. int z3 = d3(4, 3);
  22.  
  23. Console.WriteLine(z1);
  24. Console.WriteLine(z2);
  25. Console.WriteLine(z3);
  26. }
  27. }
  28. //output:
  29. 6
  30. 9
  31. 12

Lamda expression as an Event Handler

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

No comments:

Post a Comment