Skip to content

Commit c641e7c

Browse files
BillWagnerjskeet
authored andcommitted
Add non-readonly property accessors.
Make two changes to cover property accessors in structs and the `readonly` modifier. First, in expressions, add a note that property accessors are classified as function member access: A property access invokes the corresponding function member. So, the same rules apply. Second, in structs differences, add that accessing a non-readonly setter when that requires creating a temporary is a compile-time error. In the case of a getter, the temporary is created if the getter is not classified as `readonly`.
1 parent 5b5e16b commit c641e7c

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

standard/expressions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,10 @@ The run-time processing of a function member invocation consists of the followin
12231223
- `M` is invoked.
12241224
- Otherwise, if the type of `E` is a value-type `V`, and `M` is declared or overridden in `V`:
12251225
- `E` is evaluated. If this evaluation causes an exception, then no further steps are executed. For an instance constructor, this evaluation consists of allocating storage (typically from an execution stack) for the new object. In this case `E` is classified as a variable.
1226-
- If `E` is not classified as a variable, or if `V` is not a readonly struct type ([§16.2.2](structs.md#1622-struct-modifiers)) and the declaration of `M` does not include the `readonly` modifier16.4.12), and `E` is one of:
1226+
- If `E` is not classified as a variable, or if `V` is not a readonly struct type ([§16.2.2](structs.md#1622-struct-modifiers)) and `M` is not a readonly function member16.4.12), and `E` is one of:
12271227
- an input parameter ([§15.6.2.3.2](classes.md#156232-input-parameters)), or
12281228
- a `readonly` field ([§15.5.3](classes.md#1553-readonly-fields)), or
12291229
- a `readonly` reference variable or return ([§9.7](variables.md#97-reference-variables-and-returns)),
1230-
12311230
then a temporary local variable of `E`’s type is created and the value of `E` is assigned to that variable. `E` is then reclassified as a reference to that temporary local variable. The temporary variable is accessible as `this` within `M`, but not in any other way. Thus, only when `E` can be written is it possible for the caller to observe the changes that `M` makes to `this`.
12321231
- The argument list is evaluated as described in [§12.6.2](expressions.md#1262-argument-lists).
12331232
- `M` is invoked. The variable referenced by `E` becomes the variable referenced by `this`.
@@ -1242,6 +1241,8 @@ The run-time processing of a function member invocation consists of the followin
12421241
- Otherwise, `M` is a non-virtual function member, and the function member to invoke is `M` itself.
12431242
- The function member implementation determined in the step above is invoked. The object referenced by `E` becomes the object referenced by this.
12441243
1244+
> *Note:* §12.2 classifies property access as invoking the corresponding function member, either the `get` accessor or the `set` accessor. The process above is followed for invoking that accessor. *end note*
1245+
12451246
The result of the invocation of an instance constructor ([§12.8.17.2](expressions.md#128172-object-creation-expressions)) is the value created. The result of the invocation of any other function member is the value, if any, returned ([§13.10.5](statements.md#13105-the-return-statement)) from its body.
12461247
12471248
#### 12.6.6.2 Invocations on boxed instances

standard/structs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ It is a compile-time error for a property to have a readonly modifier on all of
516516
517517
> *Note*: To correct the error, move the modifier from the accessors to the property itself. *end note*
518518
519+
For a property accessor expression, `s.P`:
520+
521+
- It is a compile-time error if `s.P` invokes the set accessor `M` of type `T` when the process in §12.6.6.1 would create a temporary copy of `s`.
522+
- If `s.P` invokes the get accessor of type `T`, the process in §12.6.6.1 is followed, including creating a temporary copy of `s` if required.
523+
519524
Automatically implemented properties ([§15.7.4](classes.md#1574-automatically-implemented-properties)) use hidden backing fields, which are only accessible to the property accessors.
520525
521526
> *Note*: This access restriction means that constructors in structs containing automatically implemented properties often need an explicit constructor initializer where they would not otherwise need one, to satisfy the requirement of all fields being definitely assigned before any function member is invoked or the constructor returns. *end note*

0 commit comments

Comments
 (0)