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

The Evolutionary Guide to C# Lambda Syntax

, 7 Jun 2012
Rate this:
Please Sign up or sign in to vote.
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.

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.

button1.Click += button1_Click;

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

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

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

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.

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:

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.

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:

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

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

License

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

About the Author

James Curran
Web Developer AFS Technologies
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
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 2 Pinmembermbsmbs10-Jun-12 21:50 
GeneralExcellent Explaination PinmemberBruce Lee Harrison8-Jun-12 6:46 
GeneralMy vote of 5 PinmemberPrasanta_Prince8-Jun-12 2:17 
GeneralMy vote of 5 PinmemberBigTimber@home7-Jun-12 13:32 
GeneralMy vote of 5 Pinmemberjpmik7-Jun-12 12:19 

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 | Mobile
Web03 | 2.8.140718.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by James Curran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid