Video: Understanding Lambda Expressions (C# Programming Guide)

In the following video cast, you will get a quick introduction to "Lambda Expressions""A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types," stated Microsoft."All lambda expressions use the lambda operator =>, which is read as "goes to". The left side […]

In the following video cast, you will get a quick introduction to "Lambda Expressions"

"A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types," stated Microsoft.

"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 * x is read "x goes to x times x."

The => operator has the same precedence as assignment (=) and is right-associative.

Lambdas are used in method-based LINQ queries as arguments to standard query operator methods such as "Where."

When you use method-based syntax to call the Where method in the Enumerable class (as you do in LINQ to Objects and LINQ to XML) the parameter is a delegate type System.Func(Of T, TResult). A lambda expression is the most convenient way to create that delegate. When you call the same method in, for example, the System.Linq.Queryable class (as you do in LINQ to SQL) then the parameter type is an System.Linq.Expressions.Expression<Func> where Func is any Func delegates with up to sixteen input parameters. Again, a lambda expression is just a very concise way to construct that expression tree. The lambdas allow the Where calls to look similar although in fact the type of object created from the lambda is different.

Lambdas are not allowed on the left side of the is or as operator."