• Create Account

### #ActualNypyren

Posted 28 November 2012 - 11:55 AM

I always did stuff like this. The first time I heard of lambda expressions was from your post and although I read the msdn article about lambda expressions by now I'm still a bit lost. You say I could use any of those forms, but do the prior ones do anything better than the standard form?

Yes. The Lambda and anonymous methods are allowed to "capture" or "close over" variables in scope, which isn't available with a standard function.

This is most often used when you're using lambdas in LINQ:

Console.WriteLine("Please enter a search word:");

List<Record> matches = allRecords.Where(record => record.Keywords.Contains(word)).ToList(); // "word" is being captured by the lambda


In the above example, it's not immediately apparent how powerful capturing variables is. The variables are captured EVEN AFTER they go out of scope of the function that declared them!

This means you can do things like this:

Button[] buttons = new Button[10];

for (int i=0; i<buttons.Length; ++i)
{
int buttonID = i; // NOTE: If you capture a variable incremented by a loop, bad things happen!  You must assign a separate variable, otherwise all lambdas will capture a single variable.
buttons[i] = new Button();
buttons[i].Click += delegate
{
MessageBox.Show("Button " + buttonID + " was clicked!"); // If I use "i" here instead of "buttonID", all buttons would print "Button 10 was clicked!".
};
}


And the button handlers will still work even after the function which contained this code exits.

### #2Nypyren

Posted 28 November 2012 - 11:55 AM

I always did stuff like this. The first time I heard of lambda expressions was from your post and although I read the msdn article about lambda expressions by now I'm still a bit lost. You say I could use any of those forms, but do the prior ones do anything better than the standard form?

Yes. The Lambda and anonymous methods are allowed to "capture" or "close over" variables in scope, which isn't available with a standard function.

This is most often used when you're using lambdas in LINQ:

Console.WriteLine("Please enter a search word:");

List<Record> matches = allRecords.Where(record => record.Keywords.Contains(word)).ToList(); // "word" is being captured by the lambda


In the above example, it's not immediately apparent how powerful capturing variables is. The variables are captured EVEN AFTER they go out of scope of the function that declared them!

This means you can do things like this:

Button[] buttons = new Button[10];

for (int i=0; i<buttons.Length; ++i)
{
int buttonID = i; // NOTE: If you capture a variable incremented by a loop, bad things happen!  You must assign a separate variable, otherwise all lambdas will capture a single variable.
buttons[i] = new Button();
buttons[i].Click += delegate
{
MessageBox.Show("Button " + buttonID + " was clicked!"); // If I use "i" here instead of "buttonID", all buttons would print "Button 10 was clicked!".
};
}


### #1Nypyren

Posted 28 November 2012 - 11:52 AM

I always did stuff like this. The first time I heard of lambda expressions was from your post and although I read the msdn article about lambda expressions by now I'm still a bit lost. You say I could use any of those forms, but do the prior ones do anything better than the standard form?

Yes. The Lambda and anonymous methods are allowed to "capture" or "close over" variables in scope, which isn't available with a standard function.

This is most often used when you're using lambdas in LINQ:

Console.WriteLine("Please enter a search word:");

List<Record> matches = allRecords.Where(record => record.Keywords.Contains(word)).ToList(); // "word" is being captured by the lambda


In the above example, it's not immediately apparent how powerful capturing variables is. The variables are captured EVEN BEYOND the scope of the function that declared them!

This means you can do things like this:

Button[] buttons = new Button[10];

for (int i=0; i<buttons.Length; ++i)
{
int buttonID = i; // NOTE: If you capture a variable incremented by a loop, bad things happen!  You must assign a separate variable, otherwise all lambdas will capture a single variable.
buttons[i] = new Button();
buttons[i].Click += delegate
{
MessageBox.Show("Button " + buttonID + " was clicked!"); // If I use "i" here instead of "buttonID", all buttons would print "Button 10 was clicked!".
};
}


PARTNERS