Click here to Skip to main content
11,495,371 members (62,388 online)
Click here to Skip to main content

Lambda Simplified

, 18 Oct 2010 CPOL 49.5K 73
Rate this:
Please Sign up or sign in to vote.
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:

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.
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:

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.
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:

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)

Share

About the Author

GuruprasadV
Technical Lead Microsoft
India India
f

Comments and Discussions

 
GeneralMy vote of 2 Pin
yuriygo20-Jul-14 18:42
memberyuriygo20-Jul-14 18:42 
GeneralMy vote of 5 Pin
Purushotham Agaraharam22-Apr-14 3:10
groupPurushotham Agaraharam22-Apr-14 3:10 
GeneralMy vote of 4 Pin
Henning Dieterichs4-Apr-12 9:44
memberHenning Dieterichs4-Apr-12 9:44 
GeneralMy vote of 1 Pin
Patrick Harris10-Mar-12 15:01
memberPatrick Harris10-Mar-12 15:01 
GeneralMy vote of 5 Pin
ZTS16-Jan-11 16:37
memberZTS16-Jan-11 16:37 
GeneralMy vote of 5 Pin
Steve Wellens24-Dec-10 17:52
memberSteve Wellens24-Dec-10 17:52 
GeneralMy vote of 1 Pin
Dan Neely22-Nov-10 4:09
memberDan Neely22-Nov-10 4:09 
GeneralMy vote of 1 Pin
Keith Barrow21-Nov-10 2:25
memberKeith Barrow21-Nov-10 2:25 
GeneralMy vote of 1 Pin
OriginalGriff20-Nov-10 23:03
memberOriginalGriff20-Nov-10 23:03 
GeneralMisleading Pin
Pete O'Hanlon20-Nov-10 13:34
mvpPete O'Hanlon20-Nov-10 13:34 
GeneralRe: Misleading Pin
Nishant Sivakumar20-Nov-10 14:34
mvpNishant Sivakumar20-Nov-10 14:34 
GeneralRe: Misleading Pin
Pete O'Hanlon21-Nov-10 5:37
mvpPete O'Hanlon21-Nov-10 5:37 
GeneralMy vote of 1 Pin
Oussema Zarrai19-Nov-10 6:50
memberOussema Zarrai19-Nov-10 6:50 
GeneralMy Vote of 1 Pin
Oussema Zarrai19-Nov-10 6:50
memberOussema Zarrai19-Nov-10 6:50 
GeneralMy vote of 1 Pin
Pranay Rana17-Nov-10 18:40
memberPranay Rana17-Nov-10 18:40 
GeneralMy vote of 4 Pin
Anurag Gandhi15-Nov-10 19:31
memberAnurag Gandhi15-Nov-10 19:31 
GeneralMy vote of 5 Pin
Eddy Vluggen14-Nov-10 1:57
mvpEddy Vluggen14-Nov-10 1:57 
GeneralMy vote of 2 Pin
DenisK13-Nov-10 11:00
memberDenisK13-Nov-10 11:00 
GeneralMy vote of 1 Pin
Izzet Kerem Kusmezer9-Nov-10 1:44
memberIzzet Kerem Kusmezer9-Nov-10 1:44 
GeneralMy vote of 5 Pin
Eswa8-Nov-10 22:47
memberEswa8-Nov-10 22:47 
Rant[My vote of 1] My vote of 1 Pin
Ivan A. Gusev31-Oct-10 9:34
memberIvan A. Gusev31-Oct-10 9:34 
GeneralMy vote of 5 Pin
Bilal Haider27-Oct-10 21:37
memberBilal Haider27-Oct-10 21:37 
GeneralRe: My vote of 5 Pin
Chang tzuhsun8-Dec-10 22:14
memberChang tzuhsun8-Dec-10 22:14 
GeneralOne line does not understanding bring Pin
robvon26-Oct-10 10:00
memberrobvon26-Oct-10 10:00 
GeneralNice article Pin
David Catriel26-Oct-10 4:07
memberDavid Catriel26-Oct-10 4:07 
GeneralGood topic but needs more study Pin
Suchi Banerjee, Pune25-Oct-10 19:41
memberSuchi Banerjee, Pune25-Oct-10 19:41 
GeneralRe: Good topic but needs more study Pin
GuruprasadV25-Oct-10 21:21
memberGuruprasadV25-Oct-10 21:21 
GeneralMy vote of 5 Pin
Kashif ilyas25-Oct-10 19:39
memberKashif ilyas25-Oct-10 19:39 
GeneralMy vote of 1 Pin
MR_SAM_PIPER25-Oct-10 14:56
memberMR_SAM_PIPER25-Oct-10 14:56 
GeneralRe: My vote of 1 Pin
ramuknavap25-Oct-10 22:06
memberramuknavap25-Oct-10 22:06 
GeneralMy vote of 5 Pin
leonardo brambilla25-Oct-10 12:36
memberleonardo brambilla25-Oct-10 12:36 
GeneralMy vote of 1 Pin
Argyle4Ever25-Oct-10 5:15
memberArgyle4Ever25-Oct-10 5:15 
GeneralVote of 1 Pin
jfriedman21-Oct-10 8:07
memberjfriedman21-Oct-10 8:07 
GeneralRe: Vote of 1 Pin
S. Senthil Kumar21-Oct-10 21:05
memberS. Senthil Kumar21-Oct-10 21:05 
GeneralMy vote of 1 Pin
Josh Fischer20-Oct-10 6:59
mvpJosh Fischer20-Oct-10 6:59 
GeneralMy vote of 1 Pin
Seishin#19-Oct-10 22:41
memberSeishin#19-Oct-10 22:41 
GeneralDifferent(good) approach for Lambda Pin
ChandraMuralis19-Oct-10 5:26
memberChandraMuralis19-Oct-10 5:26 
GeneralMy vote of 4 Pin
ChandraMuralis19-Oct-10 5:25
memberChandraMuralis19-Oct-10 5:25 
GeneralGood informative tip, Not an article Pin
Hiren Solanki18-Oct-10 21:13
memberHiren Solanki18-Oct-10 21:13 
GeneralRe: Good informative tip, Not an article Pin
GuruprasadV18-Oct-10 23:13
memberGuruprasadV18-Oct-10 23:13 
GeneralRe: Good informative tip, Not an article Pin
Hiren Solanki18-Oct-10 23:27
memberHiren Solanki18-Oct-10 23:27 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 19 Oct 2010
Article Copyright 2010 by GuruprasadV
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid