Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
C Sharp Lambda Expression

C Sharp Lambda Expression

23 May 2024
Intermediate
135K Views
7 min read
Learn via Video Course & by Doing Hands-on Labs

Free C# Course Online

Lambda Expression in C#

The concept of lambda expression was introduced in C# 3.0. Basically, Lambda expression is a more concise syntax of anonymous method. It is just a new way to write anonymous methods. At compile time all the lambda expressions are converted into anonymous methods according to lambda expression conversion rules. The left side of the lambda 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 Lambda Expression

  1. Statement Lambda

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

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

    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 

Read More: C# Interview Questions And Answers

Features of the Lambda Expression

  1. Lambda expressions themselves do not have type. 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 in C# Compiler


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

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

 //anonymous method using statement lambda
 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

Lambda 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 Lambda expression"; };
 }
 protected void ClickEvent(object sender, EventArgs e)
 {
 lblmsg.Text="Reset Button clicked using Regular method";
 }
 

Summary

In this article I try to expose lambda expression with simple example. I hope after reading this article you will be able to use lambda 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.

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Microsoft Azure Cloud Architect Aug 11 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this