Click here to Skip to main content
15,886,037 members
Articles / Programming Languages / C#

The Evolutionary Guide to C# Lambda Syntax

Rate me:
Please Sign up or sign in to vote.
4.87/5 (9 votes)
7 Jun 2012CPOL2 min read 11.5K   14   5
All you need to know to write a lambda function.

Originally (.NET V1.1), we had to explicitly create a Delegate object to wrap a method reference to use it as a callback method, and that method had to named.

C#
button1.Click += new EventHandler(button1_Click);
// :
// :
void button1_Click(object sender, EventArgs e)
{
    DoStuff();
}

With .NET v2.0, the C# compiler got smart enough to realize that when I used a method reference in code, I needed it wrapped up as a delegate, so it would silently to that for me.

C#
button1.Click += button1_Click;

Better still, C#2 added anonymous methods, which could be written inline.

C#
button1.Click += delegate (object s, EventArgs ea) { DoStuff();}

Then, C#3, we got lambdas, which were basically anonymous methods with a cleaned up syntax.

C#
button1.Click += (object s, EventArgs ea) => { DoStuff(); };

But, at the same time, the compiler got brighter about figuring things out for itself.  For example, the Button Click event took a delegate to a method which had an object and an EventArgs parameter. Giving it anything else is a compile-time error.  So, since we all agree that those are the parameters, why is it necessary for us to stand that out loud.  Why not just let the compile assume it.

C#
button1.Click += (s, ea) => { DoStuff(); };

From there, we have just a few more refinements, for special (but common) cases, but for these we can no longer use Button Click as the destination of our method reference, so from here on out, we’ll be use Enumerator.Where on an int array. The important point here is that the lambda we will be writing takes an int, and return a bool. Within that environment, our last syntax would look like this:

C#
int[] x = new int[] {1,2,3,4};
var y = x.Where((x)=>{return x % 2 == 0;}).ToList();

But, if we have just one parameter, the compiler can figure out where it starts and ends, so we don’t need the parenthesis.

C#
var y = x.Where( x => { return x % 2 == 0;}).ToList();

Finally, if all the function does is return a value (which is all a true lambda function is supposed to do), we can eliminate the curly braces and even the return:

C#
var y = x.Where( x =>  x % 2 == 0).ToList();

And that’s really all you need to know to write a lambda function.

This article was originally posted at http://feeds.feedburner.com/HonestIllusion

License

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


Written By
Software Developer (Senior) NovelTheory LLC
United States United States
20+ years as a developer : Assembly, C, C++ and C# (in that order) with sidelines in ASP/VBScript, ASP.Net, JavaScript, Perl, QuickBasic, VisualBasic, plus a few others which I'm not going to mention because if I did someone might ask me to use them again (shudder)

Microsoft MVP in VC++ (1994-2004)

I also run www.NJTheater.com as a hobby.

Full resume & stuff at NovelTheory.com

Underused blog at HonestIllusion.com

Comments and Discussions

 
GeneralMy vote of 2 Pin
MB Seifollahi10-Jun-12 21:50
professionalMB Seifollahi10-Jun-12 21:50 
GeneralExcellent Explaination Pin
Bruce Lee Harrison8-Jun-12 6:46
Bruce Lee Harrison8-Jun-12 6:46 
GeneralMy vote of 5 Pin
Prasanta_Prince8-Jun-12 2:17
Prasanta_Prince8-Jun-12 2:17 
Good one
GeneralMy vote of 5 Pin
BigTimber@home7-Jun-12 13:32
professionalBigTimber@home7-Jun-12 13:32 
GeneralMy vote of 5 Pin
jpmik7-Jun-12 12:19
jpmik7-Jun-12 12:19 

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.