Lambda expressions are one of the cool
new features of .NET 3.5 and are available in the C# compiler inside Visual
Studio 2008. They can make code more readable and permit the framework to
provide some really neat functionality when mixed with other .NET features like
generics and anonymous methods.
A lambda expression is a shorthand syntax for an anonymous method. All lambda expressions use
the lambda operator =>, which is read as “goes to”. The left side of the
lambda operator specifies the input parameters and the right side holds the
expression or statement block.
Anonymous Method
An anonymous method is a method body that
is defined in place, rather than given a name
and called by name elsewhere.
Here are some lambda expressions:
i
=> i * I
(i1,
i2) => i1 + i2
parm
=> MessageBox.Show(
"Do
you want to save the file: " + parm + "?",
"Confirm
file save", MessageBoxButtons.YesNo)
In the above expressions, the presence of
the =>
operator makes it a lambda expression.
On the left side of the expression
is a parameter list. On the right side is an expression to be
evaluated. The expression may contain the parameters that were defined on
the other side of the => operator.
In essence, lambda expressions define a
function's parameters and its body. It has the limitation that the body must
be a single expression which returns a single object.
They can be used anywhere a delegate would be used. They are callback
methods.
Example:
delegate int anonymousDel(int
i);
anonymousDel myDelegate = new anonymousDel(
delegate(int x)
{
return x * 2;
});
private void btnDisp_Click(object sender, EventArgs e)
anonymousDel myDelegate = new anonymousDel(
delegate(int x)
{
return x * 2;
});
private void btnDisp_Click(object sender, EventArgs e)
{
MessageBox.Show
(" "+ myDelegate(5));
}
The segment inside the braces of anonymousDel is also called as Anonymous Function.
Now lets convert it to Lambda Expression:
Now lets convert it to Lambda Expression:
anonymousDel myDelegate = x => x * 2;
x => x * 2 is the Expression that is known as Lambda Expression. All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * 2 is read "x goes to 2 times x."
The above expression can be generalize as
(input parameters) => Expression;
and can be termed as "Expression Lambdas" since single Expression is involved here.
A Lambda Expression can contain multiple statements and can be surrounded by
{ } just like anonymous functions.
(input param1, input param2) => { statement1, Statement 2};
For e.g.
anonymousDel2 getBigInteger = (x, y) => { if (x > y) return x; else return y; };
Console.WriteLine(getBigInteger(10,15));
(input param1, input param2) => { statement1, Statement 2};
For e.g.
anonymousDel2 getBigInteger = (x, y) => { if (x > y) return x; else return y; };
Console.WriteLine(getBigInteger(10,15));
LINQ to Objects
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
LINQ to SQL
Customers.Where(c => c.City == "London");
LINQ To XML
var xml1 = element.Descendants("customer").Where(cust => cust.Attribute("id").Value == CustomerId).SingleOrDefault();
OTHER
Func<>, Action<> and Expression<> are the new generic delegates where you can use Lambda Expressions.
Look at the below code snippet:
delegate int anonymousDel2(int a, int b);
anonymousDel2 getBigInteger = (x, y) => { if (x > y) return x; else return y; };
Console.WriteLine(getBigInteger(10,15));
Lets modify the above code snippet using generic delegates and Lambda Expressions
Func<int, int, int> getBigInteger = (x, y) => { if (x > y) return x; else return y; };
Console.WriteLine(getBigInteger(10,15));
0 comments:
Post a Comment