Lambda expressions are a simpler was introduced in C# 3.0 syntax for anonymous delegates.lambda expressions can be converted to expression trees which allows for a lot of the magic like LINQ to SQL.It is just a new way to write anonymous methods. At compile time all the lamda expressions are converted into anonymous methods according to lamda expression conversion rules.The left side of the lamda operator "=>" represents the arguments to the method and the right side is the method body.
Lambda expressions provide a concise, functional syntax for writing anonymous methods. They are super useful when writing LINQ query expressions as they provide a very compact and type-safe way to write functions that can be passed as arguments for subsequent evaluation. Lambda Expressions were introduced in the .NET Framework 3.5.
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." This expression can be assigned to a delegate type as follows:
delegate int del(int i); static void Main(string[] args) { del myDelegate = x => x * x; int j = myDelegate(5); //j = 25 }To create an expression tree type:using System.Linq.Expressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Expression<del> myET = x => x * 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.Notice that the delegate signature has one implicitly-typed input parameter of type int, and returns an int. The lambda expression can be converted to a delegate of that type because it also has one input parameter (x) and a return value that the compiler can implicitly convert to type int. (Type inference is discussed in more detail in the following sections.) When the delegate is invoked by using an input parameter of 5, it returns a result of 25.Lambdas are not allowed on the left side of the is or as operator.All restrictions that apply to anonymous methods also apply to lambda expressions.
delegate int del(int i); static void Main(string[] args) { del myDelegate = x => x * x; int j = myDelegate(5); //j = 25 }
To create an expression tree type:
using System.Linq.Expressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Expression<del> myET = x => x * 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.Notice that the delegate signature has one implicitly-typed input parameter of type int, and returns an int. The lambda expression can be converted to a delegate of that type because it also has one input parameter (x) and a return value that the compiler can implicitly convert to type int. (Type inference is discussed in more detail in the following sections.) When the delegate is invoked by using an input parameter of 5, it returns a result of 25.Lambdas are not allowed on the left side of the is or as operator.All restrictions that apply to anonymous methods also apply to lambda expressions.
using System.Linq.Expressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Expression<del> myET = x => x * 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.
Notice that the delegate signature has one implicitly-typed input parameter of type int, and returns an int. The lambda expression can be converted to a delegate of that type because it also has one input parameter (x) and a return value that the compiler can implicitly convert to type int. (Type inference is discussed in more detail in the following sections.) When the delegate is invoked by using an input parameter of 5, it returns a result of 25.
Lambdas are not allowed on the left side of the is or as operator.
All restrictions that apply to anonymous methods also apply to lambda expressions.