From 22dea2f9280d4742966140b01a0e5468f6f0424d Mon Sep 17 00:00:00 2001 From: Khushal Gupta Date: Mon, 1 Oct 2018 13:41:16 +0530 Subject: [PATCH] Create Lambdaexpressions.md New file added related to lambda expressions in C#. --- .../getting-started/lambdaexpressions.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 content/csharp/getting-started/lambdaexpressions.md 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).