Skip to content

Commit 7808852

Browse files
author
Ron Petrusha
authored
Lambda operator and expression body definition (dotnet#3286)
* Lambda operator and expression body definition * Addressed review comments
1 parent d3976d7 commit 7808852

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

docs/csharp/language-reference/operators/lambda-operator.md

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "=> Operator (C# Reference)"
3-
ms.date: "2015-07-20"
3+
ms.date: "2017-10-02"
44
ms.prod: .net
55
ms.technology:
66
- "devlang-csharp"
@@ -13,8 +13,6 @@ helpviewer_keywords:
1313
- "lambda operator [C#]"
1414
- "=> operator [C#]"
1515
- "lambda expressions [C#], => operator"
16-
ms.assetid: 8c899251-dafa-4594-bec7-243b39072880
17-
caps.latest.revision: 21
1816
author: "BillWagner"
1917
ms.author: "wiwagn"
2018
translation.priority.ht:
@@ -34,6 +32,15 @@ translation.priority.mt:
3432
- "tr-tr"
3533
---
3634
# => 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+
3744
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).
3845

3946
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.
@@ -59,7 +66,7 @@ Console.WriteLine(shortestWordLength2);
5966
// 5
6067
```
6168

62-
## Remarks
69+
### Remarks
6370
The `=>` operator has the same precedence as the assignment operator (`=`) and is right-associative.
6471

6572
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.
@@ -68,7 +75,7 @@ Console.WriteLine(shortestWordLength2);
6875
int shortestWordLength = words.Min((string w) => w.Length);
6976
```
7077

71-
## Example
78+
### Example
7279
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.
7380

7481
```csharp
@@ -93,8 +100,35 @@ static void Main(string[] args)
93100
// nine
94101
}
95102
```
96-
103+
## Expression body definition
104+
105+
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:
115+
116+
```csharp
117+
public override string ToString() => $"{fname} {lname}".Trim();
118+
```
119+
120+
It is a shorthand version of the following method definition:
121+
122+
```csharp
123+
public override string ToString()
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).
129+
97130
## See Also
98-
[C# Reference](../../../csharp/language-reference/index.md)
99-
[C# Programming Guide](../../../csharp/programming-guide/index.md)
100-
[Lambda Expressions](../../../csharp/programming-guide/statements-expressions-operators/lambda-expressions.md)
131+
[C# Reference](../../../csharp/language-reference/index.md)
132+
[C# Programming Guide](../../../csharp/programming-guide/index.md)
133+
[Lambda Expressions](../../../csharp/programming-guide/statements-expressions-operators/lambda-expressions.md)
134+
[Expression-bodied members](../../programming-guide/statements-expressions-operators/expression-bodied-members.md).

0 commit comments

Comments
 (0)