diff --git a/content/csharp/getting-started/lambdaexpressions.md b/content/csharp/getting-started/lambdaexpressions.md new file mode 100644 index 0000000..f44054b --- /dev/null +++ b/content/csharp/getting-started/lambdaexpressions.md @@ -0,0 +1,33 @@ +# Lambda Expressions + +A lambda expression is an [anonymous function](anonymous-methods.md) that you can use to create [delegates](../delegates/using-delegates.md) or [expression tree](../concepts/expression-trees/index.md) types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls. Lambda expressions are particularly helpful for writing LINQ query expressions. + +To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator [=>](../../../csharp/language-reference/operators/lambda-operator.md), and you put the expression or statement block on the other side. For example, the lambda expression `x => x * x` specifies a parameter that’s named `x` and returns the value of `x` squared. You can assign this expression to a delegate type, as the following example shows: + +```csharp +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: + +```csharp +using System.Linq.Expressions; + +namespace ConsoleApplication1 +{ + class Program + { + static void Main(string[] args) + { + Expression myET = x => x * x; + } + } +} +``` + + The `=>` operator has the same precedence as assignment (`=`) and is [right associative](../../../csharp/programming-guide/statements-expressions-operators/operators.md) (see "Associativity" section of the Operators article).