Skip to content

Documentation for expression-bodied members in C# 7#2101

Merged
rpetrusha merged 5 commits intodotnet:masterfrom
rpetrusha:exp-bodied
May 15, 2017
Merged

Documentation for expression-bodied members in C# 7#2101
rpetrusha merged 5 commits intodotnet:masterfrom
rpetrusha:exp-bodied

Conversation

@rpetrusha
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

@mairaw mairaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the builds are failing, did just a quick review

In the following code example, the call to `DoWork` is an invocation expression.

```
```cs
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do not -> don't

- "C# language, expresion-bodied members"
author: "rpetrusha"
ms.author: "ronpet"
translation.priority.ht:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: end with colon


## 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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here for this xref link

@rpetrusha
Copy link
Copy Markdown
Contributor Author

Thanks, @mairaw. I've incorporated your corrections.

Copy link
Copy Markdown
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

destructor => finalizer.

member => expression;
```

where *expression* is any valid expression.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finalizer


For more information, see [Constructors (C# Programming Guide)](../classes-and-structs/constructors.md).

## Destructors
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finalizers.


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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finalizer (twice)


[!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).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say "single expression" instead of "single-line statement".

For example, a LINQ query is a single expression, but might span multiple lines.

@rpetrusha
Copy link
Copy Markdown
Contributor Author

@BillWagner, I've addressed your comments and also changed destructor -> finalizer throughout the doc set.

@BillWagner
Copy link
Copy Markdown
Member

Wow. Thanks for checking everywhere for the destructor -> finalizer change @rpetrusha

Copy link
Copy Markdown
Contributor

@mairaw mairaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there a few more contractions to be made: do not -> don't

@rpetrusha
Copy link
Copy Markdown
Contributor Author

Thanks, @mairaw. I've changed "cs" to "csharp" and replaced "do not" with "don't". I'll merge when this publishes.

@rpetrusha rpetrusha merged commit a5ed524 into dotnet:master May 15, 2017
@rpetrusha rpetrusha deleted the exp-bodied branch May 15, 2017 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants