You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
In the variable_declarator that introduces the field (by including a variable_initializer in the declaration).
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 out or ref parameter.
Attempting to assign to a readonly field or pass it as an out or ref parameter in any other context is a compile-time error.
I didn't see how this spec text disallows the following assignment to a readonly field, which occurs in an instance constructor in the same class:
error CS0191: A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)
The text was updated successfully, but these errors were encountered:
https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/classes.md#1553-readonly-fields:
I didn't see how this spec text disallows the following assignment to a readonly field, which occurs in an instance constructor in the same class:
Which in practice (SharpLab) gives:
The text was updated successfully, but these errors were encountered: