Skip to content

Commit f4d9ffe

Browse files
authored
Merge pull request #1387 from dotnet/clarify_readonly_field_assignment
Clarify that readonly fields can only be assigned on the current instance
2 parents 286b10a + 772d869 commit f4d9ffe

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

standard/classes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ As explained in [§15.3.8](classes.md#1538-static-and-instance-members), each in
17031703
When a *field_declaration* includes a `readonly` modifier, the fields introduced by the declaration are ***readonly fields***. Direct assignments to readonly fields can only occur as part of that declaration or in an instance constructor or static constructor in the same class. (A readonly field can be assigned to multiple times in these contexts.) Specifically, direct assignments to a readonly field are permitted only in the following contexts:
17041704
17051705
- In the *variable_declarator* that introduces the field (by including a *variable_initializer* in the declaration).
1706-
- For an instance field, in the instance constructors of the class that contains the field declaration; for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a readonly field as an output or reference parameter.
1706+
- For an instance field, in the instance constructors of the class that contains the field declaration, excluding local functions and anonymous functions, and only on the instance being constructed. For a static field, in the static constructor or a static field or property initializer in the class that contains the field declaration, excluding local functions and anonymous functions. These are also the only contexts in which it is valid to pass a readonly field as an output or reference parameter.
17071707
17081708
Attempting to assign to a readonly field or pass it as an output or reference parameter in any other context is a compile-time error.
17091709

0 commit comments

Comments
 (0)