Click here to Skip to main content
Click here to Skip to main content

Lambda Simplified

By , 18 Oct 2010
 

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)

About the Author

GuruprasadV
Technical Lead Microsoft
India India
Member
f

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberHenning Dieterichs4 Apr '12 - 8:44 
GeneralMy vote of 1memberPatrick Harris10 Mar '12 - 14:01 
GeneralMy vote of 5memberZTS16 Jan '11 - 15:37 
GeneralMy vote of 5memberSteve Wellens24 Dec '10 - 16:52 
GeneralMy vote of 1memberDan Neely22 Nov '10 - 3:09 
GeneralMy vote of 1memberKeith Barrow21 Nov '10 - 1:25 
GeneralMy vote of 1memberOriginalGriff20 Nov '10 - 22:03 
GeneralMisleadingmvpPete O'Hanlon20 Nov '10 - 12:34 
GeneralRe: MisleadingmvpNishant Sivakumar20 Nov '10 - 13:34 
GeneralRe: MisleadingmvpPete O'Hanlon21 Nov '10 - 4:37 
GeneralMy vote of 1memberOussema Zarrai19 Nov '10 - 5:50 
GeneralMy Vote of 1memberOussema Zarrai19 Nov '10 - 5:50 
GeneralMy vote of 1memberPranay Rana17 Nov '10 - 17:40 
GeneralMy vote of 4memberAnurag Gandhi15 Nov '10 - 18:31 
GeneralMy vote of 5mvpEddy Vluggen14 Nov '10 - 0:57 
GeneralMy vote of 2memberDenisK13 Nov '10 - 10:00 
GeneralMy vote of 1memberIzzet Kerem Kusmezer9 Nov '10 - 0:44 
GeneralMy vote of 5memberEswa8 Nov '10 - 21:47 
Rant[My vote of 1] My vote of 1memberIvan A. Gusev31 Oct '10 - 8:34 
GeneralMy vote of 5memberBilal Haider27 Oct '10 - 20:37 
GeneralRe: My vote of 5memberChang tzuhsun8 Dec '10 - 21:14 
GeneralOne line does not understanding bringmemberrobvon26 Oct '10 - 9:00 
GeneralNice articlememberDavid Catriel26 Oct '10 - 3:07 
GeneralGood topic but needs more studymemberSuchi Banerjee, Pune25 Oct '10 - 18:41 
GeneralRe: Good topic but needs more studymemberGuruprasadV25 Oct '10 - 20:21 
GeneralMy vote of 5memberKashif ilyas25 Oct '10 - 18:39 
GeneralMy vote of 1memberMR_SAM_PIPER25 Oct '10 - 13:56 
GeneralRe: My vote of 1memberramuknavap25 Oct '10 - 21:06 
GeneralMy vote of 5memberleonardo brambilla25 Oct '10 - 11:36 
GeneralMy vote of 1memberArgyle4Ever25 Oct '10 - 4:15 
GeneralVote of 1memberjfriedman21 Oct '10 - 7:07 
GeneralRe: Vote of 1memberS. Senthil Kumar21 Oct '10 - 20:05 
GeneralMy vote of 1mvpJosh Fischer20 Oct '10 - 5:59 
GeneralMy vote of 1memberSeishin#19 Oct '10 - 21:41 
GeneralDifferent(good) approach for LambdamemberChandraMuralis19 Oct '10 - 4:26 
GeneralMy vote of 4memberChandraMuralis19 Oct '10 - 4:25 
GeneralGood informative tip, Not an articlememberHiren Solanki18 Oct '10 - 20:13 
GeneralRe: Good informative tip, Not an articlememberGuruprasadV18 Oct '10 - 22:13 
GeneralRe: Good informative tip, Not an articlememberHiren Solanki18 Oct '10 - 22:27 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 19 Oct 2010
Article Copyright 2010 by GuruprasadV
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid