Click here to Skip to main content
15,878,945 members
Articles / Programming Languages / C#
Tip/Trick

Lambda Simplified

Rate me:
Please Sign up or sign in to vote.
4.41/5 (67 votes)
18 Oct 2010CPOL1 min read 96.8K   75   41
The article simplifies the understanding of Lambda expression

Introduction

Lambda expressions in C# are simple to use, but when you see Lambda expression for the first time, it will be so confusing. The article simplifies the understanding of a Lambda expression by taking you through a code evolution tour.

Evolution

In a typical .NET 1.0 code, the simple event handler can be written as follows:

C#
public Form1()
{
   InitializeComponent();
   this.button1.Click += new System.EventHandler(this.button1_Click);
}

private void button1_Click(object sender, EventArgs e)
{
   MessageBox.Show("Hello Events");
}

By using .NET 2.0 Anonymous method, we can simplify the code by:

  • Removing method name
  • Bringing it closer to event handler (move the code within the {})
  • No need to add Event Handler. You can use Delegate keyword to create a delegate object of the anonymous method.
C#
public Form1()
{
   InitializeComponent();
   this.button1.Click += new System.EventHandler(this.button1_Click);

    private void button1_Click delegate (object sender, EventArgs e)
    {
        MessageBox.Show("Hello Events");
    }
}

So if you use anonymous method, the code looks like:

C#
public Form1()
{
   InitializeComponent();
   this.button1.Click += delegate (object sender, EventArgs e)
                         {
                             MessageBox.Show("Hello Events");
                         };
}

By using .NET 3.0 lambda syntax, the code is even more simplified.

  • You can remove delegate keyword
  • No need to explicitly state the param types, as the compiler can inference type types
  • When we have single expression, no need for {} also.
  • and introduce => to split the param and method expression.
C#
public Form1()
{
   InitializeComponent();
   this.button1.Click += delegate (object sender, EventArgs e) =>
                         {
                             MessageBox.Show("Hello Events");
                         };
}

So now with Lambda syntax, the same code simplified as below:

C#
public Form1()
{
   InitializeComponent();
   this.button1.Click += (sender, e) => MessageBox.Show("Hello Events");
 }

Conclusion

The article try to simplify the understanding of Lambda expression. To know more about Lambda, check this MSDN article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Microsoft
India India
f

Comments and Discussions

 
GeneralMy vote of 2 Pin
yuriygo20-Jul-14 17:42
yuriygo20-Jul-14 17:42 
GeneralMy vote of 5 Pin
Purushotham Agaraharam22-Apr-14 2:10
Purushotham Agaraharam22-Apr-14 2:10 
GeneralMy vote of 4 Pin
Henning Dieterichs4-Apr-12 8:44
Henning Dieterichs4-Apr-12 8:44 
GeneralMy vote of 1 Pin
Patrick Harris10-Mar-12 14:01
Patrick Harris10-Mar-12 14:01 
GeneralMy vote of 5 Pin
ZTS16-Jan-11 15:37
ZTS16-Jan-11 15:37 
GeneralMy vote of 5 Pin
Steve Wellens24-Dec-10 16:52
Steve Wellens24-Dec-10 16:52 
GeneralMy vote of 1 Pin
Dan Neely22-Nov-10 3:09
Dan Neely22-Nov-10 3:09 
GeneralMy vote of 1 Pin
Keith Barrow21-Nov-10 1:25
professionalKeith Barrow21-Nov-10 1:25 
GeneralMy vote of 1 Pin
OriginalGriff20-Nov-10 22:03
mveOriginalGriff20-Nov-10 22:03 
GeneralMisleading Pin
Pete O'Hanlon20-Nov-10 12:34
mvePete O'Hanlon20-Nov-10 12:34 
GeneralRe: Misleading Pin
Nish Nishant20-Nov-10 13:34
sitebuilderNish Nishant20-Nov-10 13:34 
GeneralRe: Misleading Pin
Pete O'Hanlon21-Nov-10 4:37
mvePete O'Hanlon21-Nov-10 4:37 
GeneralMy vote of 1 Pin
Oussema Zarrai19-Nov-10 5:50
Oussema Zarrai19-Nov-10 5:50 
GeneralMy Vote of 1 Pin
Oussema Zarrai19-Nov-10 5:50
Oussema Zarrai19-Nov-10 5:50 
GeneralMy vote of 1 Pin
Pranay Rana17-Nov-10 17:40
professionalPranay Rana17-Nov-10 17:40 
GeneralMy vote of 4 Pin
Anurag Gandhi15-Nov-10 18:31
professionalAnurag Gandhi15-Nov-10 18:31 
GeneralMy vote of 5 Pin
Eddy Vluggen14-Nov-10 0:57
professionalEddy Vluggen14-Nov-10 0:57 
GeneralMy vote of 2 Pin
DenisK13-Nov-10 10:00
professionalDenisK13-Nov-10 10:00 
GeneralMy vote of 1 Pin
Izzet Kerem Kusmezer9-Nov-10 0:44
Izzet Kerem Kusmezer9-Nov-10 0:44 
Useless
GeneralMy vote of 5 Pin
TweakBird8-Nov-10 21:47
TweakBird8-Nov-10 21:47 
Rant[My vote of 1] My vote of 1 Pin
Ivan A. Gusev31-Oct-10 8:34
Ivan A. Gusev31-Oct-10 8:34 
GeneralMy vote of 5 Pin
Bilal Haider27-Oct-10 20:37
professionalBilal Haider27-Oct-10 20:37 
GeneralRe: My vote of 5 Pin
Chang tzuhsun8-Dec-10 21:14
Chang tzuhsun8-Dec-10 21:14 
GeneralOne line does not understanding bring Pin
robvon26-Oct-10 9:00
robvon26-Oct-10 9:00 
GeneralNice article Pin
David Catriel26-Oct-10 3:07
David Catriel26-Oct-10 3:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.