Click here to Skip to main content
Licence CPOL
First Posted 17 Oct 2010
Views 30,928
Bookmarked 61 times

Lambda Simplified

By | 18 Oct 2010 | Article
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)

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberHenning Dieterichs8:44 4 Apr '12  
GeneralMy vote of 1 PinmemberPatrick Harris14:01 10 Mar '12  
GeneralMy vote of 5 PinmemberZTS15:37 16 Jan '11  
GeneralMy vote of 5 PinassociateSteve Wellens16:52 24 Dec '10  
GeneralMy vote of 1 PinmemberDan Neely3:09 22 Nov '10  
GeneralMy vote of 1 PinmemberKeith Barrow1:25 21 Nov '10  
GeneralMy vote of 1 PinmemberOriginalGriff22:03 20 Nov '10  
GeneralMisleading PinmvpPete O'Hanlon12:34 20 Nov '10  
GeneralRe: Misleading PinmvpNishant Sivakumar13:34 20 Nov '10  
GeneralRe: Misleading PinmvpPete O'Hanlon4:37 21 Nov '10  
GeneralMy vote of 1 PinmemberOussema Zarrai5:50 19 Nov '10  
GeneralMy Vote of 1 PinmemberOussema Zarrai5:50 19 Nov '10  
GeneralMy vote of 1 PinmemberPranay Rana17:40 17 Nov '10  
GeneralMy vote of 4 PinmemberAnurag Gandhi18:31 15 Nov '10  
GeneralMy vote of 5 PinmvpEddy Vluggen0:57 14 Nov '10  
GeneralMy vote of 2 PinmemberDenisK10:00 13 Nov '10  
GeneralMy vote of 1 PinmemberIzzet Kerem Kusmezer0:44 9 Nov '10  
GeneralMy vote of 5 PinmemberEswa21:47 8 Nov '10  
Rant[My vote of 1] My vote of 1 PinmemberIvan A. Gusev8:34 31 Oct '10  
GeneralMy vote of 5 PinmemberBilal Haider20:37 27 Oct '10  
GeneralRe: My vote of 5 PinmemberChang tzuhsun21:14 8 Dec '10  
GeneralOne line does not understanding bring Pinmemberrobvon9:00 26 Oct '10  
GeneralNice article PinmemberDavid Catriel3:07 26 Oct '10  
GeneralGood topic but needs more study PinmemberSuchi Banerjee, Pune18:41 25 Oct '10  
GeneralRe: Good topic but needs more study PinmemberGuruprasadV20:21 25 Oct '10  

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.

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