I have found it difficult to discuss things such as lambda expressions with new developers. Telling someone how to write one who has never seen one can be frustrating. So here are some guidelines and text that I have found to be helpful.
A lambda is new syntax for creating delegates and expression trees. A delegate is like a pointer to a method, and a expression tree is like a conditional statement that can be processed and built at run-time.
Lambdas are also used with LINQ. You can either use LINQ method syntax (lambda), or LINQ query syntax.
A basic lambda: x => x * x
This is read as "x goes to x multiplied by x"
The "goes to" refers to the =>
If you want some humor you can call the => a boner!
When you want to open a parameterless lambda you can say "empty".
So () => x * 5 could be read as "start an empty lambda expression that multiplies x by 5"
And () => { x = x * 5; return true; } can be read as "start an empty lambda statement that sets x to x times 5 then returns true"
The important part is the "start an empty lambda", not what the method actually does. You can work on that as a separate sentence or wait till the lambda is signature is written.
(x) => x * 5 can be read as "start a lambda expression where x goes to x times 5"
Lambdas can be compared to functions, because they are at root, anonymous functions.
Signature of a method: Accessor ReturnType MethodName(Type arg1, Type arg2) {}
Signature of a lambda: (Type arg1, Type arg2) => {}
For the lambda signature, the type of arguments are optional, and so are the parenthesis if there is only one argument.
Notice there is no accessor, return type, or method name. The return type is "defined" by what is asking you for the lambda.
Lambdas can be passed in as arguments to methods, which is where it is mainly used.
There are lambda expressions and lambda statements. An expression has no return type but sometimes must evaluate to a certain type, and is a single line.
A lambda statement is multi-line and may return a value.
Lambda expression: x => x == true
Lambda statement: x => { if (x == true) return 1; else return 0; }
If a lambda takes no arguments, you use (). Ie: () => 5 * 10
Now that we’ve covered that, here are example lambdas:
(x) => x * x
x => x * x
(x, y) => x * y
() => 1 * 2
(x) =>
{
return x * x;
}
x =>
{
return x * x;
}
(x, y) =>
{
return x * y;
}
() =>
{
return 1 * 2;
}
Here are some examples of how to use lambdas:
(new List<string>() { "foo", "bar" }).Where(x => x == "foo")
(new List<string>() { "foo", "bar" }).All(x =>
{
if (x.Length > 2)
Debug.WriteLine(x);
return true;
});
session.QueryOver<Listing>()
.Where(x => x.Id == 5)
session.QueryOver<Listing>()
.OrderBy(x => x.Id).Desc
Now you can use this sample code and the way of speaking about lambdas to discuss with newer developers. Straight out telling them how they can talk freely about writing lambdas in their code helps them understand it. When things like speech are left out of explanations, because they were only given links to pages that have lambdas on them, they have issues connecting their thoughts to speech when discussing with other developers.