You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/operators/lambda-operator.md
+43-9Lines changed: 43 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: "=> Operator (C# Reference)"
3
-
ms.date: "2015-07-20"
3
+
ms.date: "2017-10-02"
4
4
ms.prod: .net
5
5
ms.technology:
6
6
- "devlang-csharp"
@@ -13,8 +13,6 @@ helpviewer_keywords:
13
13
- "lambda operator [C#]"
14
14
- "=> operator [C#]"
15
15
- "lambda expressions [C#], => operator"
16
-
ms.assetid: 8c899251-dafa-4594-bec7-243b39072880
17
-
caps.latest.revision: 21
18
16
author: "BillWagner"
19
17
ms.author: "wiwagn"
20
18
translation.priority.ht:
@@ -34,6 +32,15 @@ translation.priority.mt:
34
32
- "tr-tr"
35
33
---
36
34
# => Operator (C# Reference)
35
+
36
+
The `=>` operator can be used in two ways in C#:
37
+
38
+
- As the [lambda operator](#lamba-operator) in a [lambda expression](../../lambda-expressions.md), it separates the input variables from the lambda body.
39
+
40
+
- In an [expression body definition](#expression-body-definition), it separates a member name from the member implementation.
41
+
42
+
## Lambda operator
43
+
37
44
The `=>` token is called the lambda operator. It is used in *lambda expressions* to separate the input variables on the left side from the lambda body on the right side. Lambda expressions are inline expressions similar to anonymous methods but more flexible; they are used extensively in LINQ queries that are expressed in method syntax. For more information, see [Lambda Expressions](../../../csharp/programming-guide/statements-expressions-operators/lambda-expressions.md).
38
45
39
46
The following example shows two ways to find and display the length of the shortest string in an array of strings. The first part of the example applies a lambda expression (`w => w.Length`) to each element of the `words` array and then uses the <xref:System.Linq.Enumerable.Min%2A> method to find the smallest length. For comparison, the second part of the example shows a longer solution that uses query syntax to do the same thing.
The `=>` operator has the same precedence as the assignment operator (`=`) and is right-associative.
64
71
65
72
You can specify the type of the input variable explicitly or let the compiler infer it; in either case, the variable is strongly typed at compile time. When you specify a type, you must enclose the type name and the variable name in parentheses, as the following example shows.
The following example shows how to write a lambda expression for the overload of the standard query operator <xref:System.Linq.Enumerable.Where%2A?displayProperty=nameWithType> that takes two arguments. Because the lambda expression has more than one parameter, the parameters must be enclosed in parentheses. The second parameter, `index`, represents the index of the current element in the collection. The `Where` expression returns all the strings whose lengths are less than their index positions in the array.
An expression body definition provides a member's implementation in a highly condensed, readable form. It has the following general syntax:
106
+
107
+
```csharp
108
+
member=>expression;
109
+
```
110
+
where *expression* is a valid expression. Note that *expression* can be a *statement expression* only if the member's return type is `void`, or if the member is a constructor or a finalizer.
111
+
112
+
Expression body definitions for methods and property get statements are supported starting with C# 6. Expression body definitions for constructors, finalizers, property set statements, and indexers are supported starting with C# 7.
113
+
114
+
The following is an expression body definition for a `Person.ToString` method:
It is a shorthand version of the following method definition:
121
+
122
+
```csharp
123
+
publicoverridestringToString()
124
+
{
125
+
return$"{fname} {lname}".Trim();
126
+
}
127
+
```
128
+
For more detailed information on expression body definitions, see [Expression-bodied members](../../programming-guide/statements-expressions-operators/expression-bodied-members.md).
0 commit comments