Click here to Skip to main content
15,890,336 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 97.4K   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

 
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 
GeneralGood topic but needs more study Pin
Suchi Banerjee, Pune25-Oct-10 18:41
Suchi Banerjee, Pune25-Oct-10 18:41 
GeneralRe: Good topic but needs more study Pin
GuruprasadV25-Oct-10 20:21
GuruprasadV25-Oct-10 20:21 
GeneralMy vote of 5 Pin
xodeblack25-Oct-10 18:39
xodeblack25-Oct-10 18:39 
GeneralMy vote of 1 Pin
MR_SAM_PIPER25-Oct-10 13:56
MR_SAM_PIPER25-Oct-10 13:56 
GeneralRe: My vote of 1 Pin
ramuknavap25-Oct-10 21:06
ramuknavap25-Oct-10 21:06 
I don't agree.
Anything for the first time is quite difficult to understand but once we use for couple of months then its never an issue. Debugging is also not a issue too, i even tried to debug in multi thread scenario as well.
This article is good for a beginner ( or for people who are just trying to familiarizer in lambda expressions).

I think its a good practice to start adopting new techniques which are effective. As a matter of fact our company coding standards require us to use lambda expressions where ever possible.

At the same-time i think its a personal choice. I would prefer lambda expressions.

My vote 4
GeneralMy vote of 5 Pin
leonardo brambilla25-Oct-10 11:36
leonardo brambilla25-Oct-10 11:36 
GeneralMy vote of 1 Pin
Argyle4Ever25-Oct-10 4:15
Argyle4Ever25-Oct-10 4:15 
GeneralVote of 1 Pin
jfriedman21-Oct-10 7:07
jfriedman21-Oct-10 7:07 
GeneralRe: Vote of 1 Pin
S. Senthil Kumar21-Oct-10 20:05
S. Senthil Kumar21-Oct-10 20:05 
GeneralMy vote of 1 Pin
Josh Fischer20-Oct-10 5:59
Josh Fischer20-Oct-10 5:59 
GeneralMy vote of 1 Pin
Seishin#19-Oct-10 21:41
Seishin#19-Oct-10 21:41 
GeneralDifferent(good) approach for Lambda Pin
ChandraMuralis19-Oct-10 4:26
ChandraMuralis19-Oct-10 4:26 
GeneralMy vote of 4 Pin
ChandraMuralis19-Oct-10 4:25
ChandraMuralis19-Oct-10 4:25 
GeneralGood informative tip, Not an article PinPopular
Hiren solanki18-Oct-10 20:13
Hiren solanki18-Oct-10 20:13 
GeneralRe: Good informative tip, Not an article Pin
GuruprasadV18-Oct-10 22:13
GuruprasadV18-Oct-10 22:13 
GeneralRe: Good informative tip, Not an article PinPopular
Hiren solanki18-Oct-10 22:27
Hiren solanki18-Oct-10 22:27 

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.