Skip to content

Commit c5f9628

Browse files
RexJaeschkejskeet
authored andcommitted
Expand expression-bodied members feature
This now includes property accessors, instance constructors, static constructors and finalizers.
1 parent 73934ed commit c5f9628

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

standard/classes.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,6 +3087,7 @@ accessor_modifier
30873087
30883088
accessor_body
30893089
: block
3090+
| '=>' expression ';'
30903091
| ';'
30913092
;
30923093
```
@@ -3104,7 +3105,10 @@ The use of *accessor_modifier*s is governed by the following restrictions:
31043105
- If the property or indexer has a declared accessibility of `internal` or `protected`, the *accessor_modifier* shall be `private`.
31053106
- If the property or indexer has a declared accessibility of `private`, no *accessor_modifier* may be used.
31063107

3107-
For `abstract` and `extern` properties, the *accessor_body* for each accessor specified is simply a semicolon. A non-abstract, non-extern property may also have the *accessor_body* for all accessors specified be a semicolon, in which case it is an ***automatically implemented property*** ([§14.7.4](classes.md#1474-automatically-implemented-properties)). An automatically implemented property shall have at least a get accessor. For the accessors of any other non-abstract, non-extern property, the *accessor_body* is a *block* that specifies the statements to be executed when the corresponding accessor is invoked.
3108+
For `abstract` and `extern` properties, the *accessor_body* for each accessor specified is simply a semicolon. A non-abstract, non-extern property may also have the *accessor_body* for all accessors specified be a semicolon, in which case it is an ***automatically implemented property*** ([§14.7.4](classes.md#1474-automatically-implemented-properties)). An automatically implemented property shall have at least a get accessor. For the accessors of any other non-abstract, non-extern property, the *accessor_body* is either
3109+
3110+
- a *block* that specifies the statements to be executed when the corresponding accessor is invoked; or
3111+
- an expression body, which consists of `=>` followed by an *expression* and a semicolon, and denotes a single expression to be executed when the corresponding accessor is invoked.
31083112

31093113
A get accessor corresponds to a parameterless method with a return value of the property type. Except as the target of an assignment, when a property is referenced in an expression, the get accessor of the property is invoked to compute the value of the property ([§11.2.2](expressions.md#1122-values-of-expressions)). The body of a get accessor shall conform to the rules for value-returning methods described in [§14.6.11](classes.md#14611-method-body). In particular, all `return` statements in the body of a get accessor shall specify an expression that is implicitly convertible to the property type. Furthermore, the endpoint of a get accessor shall not be reachable.
31103114

@@ -4406,6 +4410,7 @@ constructor_initializer
44064410
44074411
constructor_body
44084412
: block
4413+
| '=>' expression ';'
44094414
| ';'
44104415
;
44114416
```
@@ -4422,7 +4427,12 @@ Each of the types referenced in the *formal_parameter_list* of an instance const
44224427

44234428
The optional *constructor_initializer* specifies another instance constructor to invoke before executing the statements given in the *constructor_body* of this instance constructor. This is described further in [§14.11.2](classes.md#14112-constructor-initializers).
44244429

4425-
When a constructor declaration includes an `extern` modifier, the constructor is said to be an ***external constructor***. Because an external constructor declaration provides no actual implementation, its *constructor_body* consists of a semicolon. For all other constructors, the *constructor_body* consists of a *block*, which specifies the statements to initialize a new instance of the class. This corresponds exactly to the *block* of an instance method with a `void` return type ([§14.6.11](classes.md#14611-method-body)).
4430+
When a constructor declaration includes an `extern` modifier, the constructor is said to be an ***external constructor***. Because an external constructor declaration provides no actual implementation, its *constructor_body* consists of a semicolon. For all other constructors, the *constructor_body* consists of either
4431+
4432+
- a *block*, which specifies the statements to initialize a new instance of the class; or
4433+
- an expression body, which consists of `=>` followed by an *expression* and a semicolon, and denotes a single expression to initialize a new instance of the class.
4434+
4435+
A *constructor_body* that is a *block* or expression body corresponds exactly to the *block* of an instance method with a `void` return type ([§14.6.11](classes.md#14611-method-body)).
44264436

44274437
Instance constructors are not inherited. Thus, a class has no instance constructors other than those actually declared in the class, with the exception that if a class contains no instance constructor declarations, a default instance constructor is automatically provided ([§14.11.5](classes.md#14115-default-constructors)).
44284438

@@ -4668,6 +4678,7 @@ static_constructor_modifiers
46684678
46694679
static_constructor_body
46704680
: block
4681+
| '=>' expression ';'
46714682
| ';'
46724683
;
46734684
```
@@ -4678,7 +4689,12 @@ A *static_constructor_declaration* may include a set of *attributes* ([§21](att
46784689

46794690
The *identifier* of a *static_constructor_declaration* shall name the class in which the static constructor is declared. If any other name is specified, a compile-time error occurs.
46804691

4681-
When a static constructor declaration includes an `extern` modifier, the static constructor is said to be an ***external static constructor***. Because an external static constructor declaration provides no actual implementation, its *static_constructor_body* consists of a semicolon. For all other static constructor declarations, the *static_constructor_body* consists of a *block*, which specifies the statements to execute in order to initialize the class. This corresponds exactly to the *method_body* of a static method with a `void` return type ([§14.6.11](classes.md#14611-method-body)).
4692+
When a static constructor declaration includes an `extern` modifier, the static constructor is said to be an ***external static constructor***. Because an external static constructor declaration provides no actual implementation, its *static_constructor_body* consists of a semicolon. For all other static constructor declarations, the *static_constructor_body* consists of either
4693+
4694+
- a *block*, which specifies the statements to execute in order to initialize the class; or
4695+
- an expression body, which consists of `=>` followed by an *expression* and a semicolon, and denotes a single expression to execute in order to initialize the class.
4696+
4697+
A *static_constructor_body* that is a *block* or expression body corresponds exactly to the *method_body* of a static method with a `void` return type ([§14.6.11](classes.md#14611-method-body)).
46824698

46834699
Static constructors are not inherited, and cannot be called directly.
46844700

@@ -4820,6 +4836,7 @@ finalizer_declaration
48204836
48214837
finalizer_body
48224838
: block
4839+
| '=>' expression ';'
48234840
| ';'
48244841
;
48254842
```
@@ -4830,7 +4847,12 @@ A *finalizer_declaration* may include a set of *attributes* ([§21](attributes.m
48304847

48314848
The *identifier* of a *finalizer_declarator* shall name the class in which the finalizer is declared. If any other name is specified, a compile-time error occurs.
48324849

4833-
When a finalizer declaration includes an `extern` modifier, the finalizer is said to be an ***external finalizer***. Because an external finalizer declaration provides no actual implementation, its *finalizer_body* consists of a semicolon. For all other finalizers, the *finalizer_body* consists of a *block*, which specifies the statements to execute in order to finalize an instance of the class. A *finalizer_body* corresponds exactly to the *method_body* of an instance method with a `void` return type ([§14.6.11](classes.md#14611-method-body)).
4850+
When a finalizer declaration includes an `extern` modifier, the finalizer is said to be an ***external finalizer***. Because an external finalizer declaration provides no actual implementation, its *finalizer_body* consists of a semicolon. For all other finalizers, the *finalizer_body* consists of either
4851+
4852+
- a *block*, which specifies the statements to execute in order to finalize an instance of the class.
4853+
- or an expression body, which consists of `=>` followed by an *expression* and a semicolon, and denotes a single expression to execute in order to finalize an instance of the class.
4854+
4855+
A *finalizer_body* that is a *block* or expression body corresponds exactly to the *method_body* of an instance method with a `void` return type ([§14.6.11](classes.md#14611-method-body)).
48344856

48354857
Finalizers are not inherited. Thus, a class has no finalizers other than the one that may be declared in that class.
48364858

0 commit comments

Comments
 (0)