Documentation for expression-bodied members in C# 7#2101
Documentation for expression-bodied members in C# 7#2101rpetrusha merged 5 commits intodotnet:masterfrom
Conversation
mairaw
left a comment
There was a problem hiding this comment.
Since the builds are failing, did just a quick review
| In the following code example, the call to `DoWork` is an invocation expression. | ||
|
|
||
| ``` | ||
| ```cs |
There was a problem hiding this comment.
the label supported by OPS is csharp
|
|
||
| The following example defines a `Person` class that overrides the <xref:System.Object.ToString</xref> method with an expression body definition. It also defines a `Show` method that displays a name to the console. Note that the `return` keyword is not used in the `ToString` expression body definition. | ||
|
|
||
| [!code-cs[expression-bodied-methods](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/expr-bodied-methods.cs#1)] |
There was a problem hiding this comment.
build is complaining about this line. the snippet code doesn't have any tags.
| If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values as listed in [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). For more information and examples, see [Instance Constructors](../../../csharp/programming-guide/classes-and-structs/instance-constructors.md). | ||
|
|
||
| Static classes and structs can also have constructors. For more information and examples, see [Static Constructors](../../../csharp/programming-guide/classes-and-structs/static-constructors.md). | ||
| If you do not provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). If you do not provide a constructor for your struct, C# replies on an *implicit default constructor* to automatically initialize each field of a value type to its default value as listed in the [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). For more information and examples, see [Instance Constructors](../../../csharp/programming-guide/classes-and-structs/instance-constructors.md). |
| - "C# language, expresion-bodied members" | ||
| author: "rpetrusha" | ||
| ms.author: "ronpet" | ||
| translation.priority.ht: |
There was a problem hiding this comment.
I don't think we need this in new articles
|
|
||
| where *expression* is any valid expression. | ||
|
|
||
| Support for expression body definitions was introduced for methods and property get accessors in C# 6 and was expanded in C# 7. Expression body definitions can be used with the type members listed in the following table. |
|
|
||
| ## Methods | ||
|
|
||
| An expression-bodied method consists of a single expression that returns a value whose type matches the method's return type, or, for methods that return `void`, that performs some operation. For example, types that override the <xref:System.Object.ToString</xref> method typically include a single expression that returns the string representation of the current object. |
There was a problem hiding this comment.
this xref link will probably fail
|
|
||
| ## Property get statements | ||
|
|
||
| If you choose to implement a property get accessor yourself, you can use an expression body definition for single-line statements that simply return the the proeprty value. Note that the `return` statement is not used. |
There was a problem hiding this comment.
the the proeprty -> the property
should get be inline code here?
is not -> isn't
|
|
||
| An expression-bodied method consists of a single expression that returns a value whose type matches the method's return type, or, for methods that return `void`, that performs some operation. For example, types that override the <xref:System.Object.ToString</xref> method typically include a single expression that returns the string representation of the current object. | ||
|
|
||
| The following example defines a `Person` class that overrides the <xref:System.Object.ToString</xref> method with an expression body definition. It also defines a `Show` method that displays a name to the console. Note that the `return` keyword is not used in the `ToString` expression body definition. |
There was a problem hiding this comment.
same here for this xref link
|
Thanks, @mairaw. I've incorporated your corrections. |
BillWagner
left a comment
There was a problem hiding this comment.
Overall, this looks great. I left a few comments. One is a recent language spec change that has C# standardizing on "finalizer" over "destructor".
The background is that "destructor" sounded too much like "deconstruction" which was added with tuples. And, both terms were used interchangeably, so picking one made sense.
|
|
||
| [!code-cs[csProgGuideObjects#86](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/destructors_1.cs)] | ||
|
|
||
| A destructor can also be implemented as an expression body definition, as the following example shows. |
There was a problem hiding this comment.
The official language spec is going to standardize on 'finalizer' over 'destructor'. (Both had been used previously.
We should match that.
| [!code-cs[expression-bodied-destructor](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/expr-bodied-destructor.cs#1)] | ||
|
|
||
| The destructor implicitly calls <xref:System.Object.Finalize%2A> on the base class of the object. Therefore, the previous destructor code is implicitly translated to the following code: | ||
| The destructor implicitly calls <xref:System.Object.Finalize%2A> on the base class of the object. Therefore, a call to a destructor is implicitly translated to the following code: |
| member => expression; | ||
| ``` | ||
|
|
||
| where *expression* is any valid expression. |
There was a problem hiding this comment.
I'd say 'a valid expression' instead of 'any valid expression'. I can't think of any disallowed off the top of my head, but I bet if I thought hard enough I could find one.
|
|
||
| ## Constructors | ||
|
|
||
| An expression body definition for a constructor typically consists of a single assignment statement or a method call that handles the constructor's arguments or initializes instance state. |
There was a problem hiding this comment.
nit: 'assignment statement' should be 'assignment expression'
|
|
||
| ## Destructors | ||
|
|
||
| An expression body definition for a destructor typically contains cleanup statements, such as statements that release unmanaged resources. |
|
|
||
| For more information, see [Constructors (C# Programming Guide)](../classes-and-structs/constructors.md). | ||
|
|
||
| ## Destructors |
|
|
||
| An expression body definition for a destructor typically contains cleanup statements, such as statements that release unmanaged resources. | ||
|
|
||
| The following example defines a destructor that uses an expression body definition to indicate that the destructor has been called. |
|
|
||
| [!code-cs[expression-bodied-destructor](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/expr-bodied-destructor.cs#1)] | ||
|
|
||
| For more information, see [Destructors (C# Programming Guide)](../classes-and-structs/destructors.md). |
There was a problem hiding this comment.
I know we haven't updated the programming guide, but what do you think of the link text as "Finalizers" instead of "Destructors"?
|
|
||
| ## Property get statements | ||
|
|
||
| If you choose to implement a property get accessor yourself, you can use an expression body definition for single-line statements that simply return the property value. Note that the `return` statement isn't used. |
There was a problem hiding this comment.
I'd say "single expression" instead of "single-line statement".
For example, a LINQ query is a single expression, but might span multiple lines.
|
@BillWagner, I've addressed your comments and also changed destructor -> finalizer throughout the doc set. |
|
Wow. Thanks for checking everywhere for the destructor -> finalizer change @rpetrusha |
mairaw
left a comment
There was a problem hiding this comment.
LGTM. There are still some snippets using the cs indentifier instead of csharp though.
|
|
||
| Read-only properties that use an expression body definition can be implemented without an explicit `set` statement. The syntax is: | ||
|
|
||
| ```cs |
There was a problem hiding this comment.
i only commented once, but all places need to switch from ```cs to ```csharp
| If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values as listed in [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). For more information and examples, see [Instance Constructors](../../../csharp/programming-guide/classes-and-structs/instance-constructors.md). | ||
|
|
||
| Static classes and structs can also have constructors. For more information and examples, see [Static Constructors](../../../csharp/programming-guide/classes-and-structs/static-constructors.md). | ||
| If you don't provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). If you do not provide a constructor for your struct, C# replies on an *implicit default constructor* to automatically initialize each field of a value type to its default value as listed in the [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). For more information and examples, see [Instance Constructors](../../../csharp/programming-guide/classes-and-structs/instance-constructors.md). |
There was a problem hiding this comment.
nit: there a few more contractions to be made: do not -> don't
|
Thanks, @mairaw. I've changed "cs" to "csharp" and replaced "do not" with "don't". I'll merge when this publishes. |
Documentation for expression-bodied members in C# 7
Summary
Documents expression body definitions, and also mentions expression body definitions in each supported member topic that did not mention it.
Also revised the top-level constructor topic somewhat.
Fixes #1789
Suggested Reviewers
@BillWagner