Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda Explanation and what it is as well as a good example [duplicate]

Tags:

c#

.net

lambda

Can anyone give me a good explanation of how to use Lambda and give a good example. I have seen it but I dont know what it is or does.

like image 913
Zyon Avatar asked Feb 25 '10 11:02

Zyon


1 Answers

A lambda expression is used to create an anonymous function. Here an anonymous function is assigned to a delegate variable:

Func<int, int> increase = (a => a + 1);

You can then use the delegate to call the function:

var answer = increase(41);

Usually lambda expressions are used to send a delegate to a method, for example sending a delegate to the ForEach method so that it's called for each element in the list:

List<int> list = new List<int>();
list.Add(1);
list.Add(2);

list.ForEach(n => Console.WriteLine(n));
like image 147
Guffa Avatar answered Nov 09 '22 13:11

Guffa