Skip to content

Commit 9590250

Browse files
RexJaeschkeBillWagner
authored andcommitted
tweak record class spec
1 parent 2057650 commit 9590250

1 file changed

Lines changed: 37 additions & 3 deletions

File tree

standard/classes.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6085,12 +6085,33 @@ A record class type shall have a public constructor whose signature corresponds
60856085
60866086
At runtime the primary constructor
60876087
6088-
1. Executes the instance initializers appearing in *record_body*
6089-
1. Invokes the base record class constructor with the arguments provided in the *record_base* clause, if present
6088+
1. Stores the value of each parameter in the corresponding generated field.
6089+
1. Executes the instance initializers appearing in *record_body*.
6090+
1. Invokes the base record class constructor with the arguments provided in the *record_base* clause, if present.
6091+
6092+
Each reference to a parameter in user code is replaced with a reference to the corresponding generated field.
60906093
60916094
If a record class has a primary constructor, any user-defined constructor, except the copy constructor, shall have an explicit `this` *constructor_initializer*.
60926095
6093-
Parameters of the primary constructor as well as members of the record class are in scope within the *argument_list* of the *record_base* clause and within initializers of instance fields or properties. Instance members would be an error in these locations, but the parameters of the primary constructor would be in scope and useable and would shadow members. Static members would also be useable.
6096+
It is an error to reference a primary constructor parameter if the reference does not occur within one of the following:
6097+
6098+
- a `nameof` argument.
6099+
- an initializer of an instance field, property or event of the declaring type.
6100+
- the `argument_list` of `class_base` of the declaring type.
6101+
- the body of an instance method of the declaring type.
6102+
- the body of an instance accessor of the declaring type.
6103+
6104+
In other words, primary constructor parameters are in scope throughout the declaring type body. They shadow members of the declaring type within an initializer of a field, property or event of the declaring type, or within the `argument_list` of `class_base` of the declaring type. They are shadowed by members of the declaring type everywhere else. Thus, in the following declaration:
6105+
6106+
```csharp
6107+
record class C(int i)
6108+
{
6109+
protected int i = i;
6110+
public int I => i;
6111+
}
6112+
```
6113+
6114+
the initializer for the field `i` references the parameter `i`, whereas the body of the property `I` references the field `i`.
60946115

60956116
A warning shall be produced if a parameter of the primary constructor is not read.
60966117

@@ -6127,6 +6148,19 @@ Expression variables declared in *argument_list* are in scope within the *argume
61276148
>
61286149
> As shown, the *constructor_initializer* of the explicit constructor is a call to the primary constructor. *end example*
61296150
6151+
A warning shall be issued on the usage of an identifier when a base member shadows a primary constructor parameter if that primary constructor parameter was not passed to the base type via its constructor.
6152+
6153+
A primary constructor parameter is considered to be passed to the base type via its constructor when all the following conditions are true for an argument in *class_base*:
6154+
6155+
- The argument represents an implicit or explicit identity conversion of a primary constructor parameter;
6156+
- The argument is not part of an expanded `params` argument;
6157+
6158+
For a primary constructor with an auto-generated field and corresponding property, attributes may be applied to those, as follows:
6159+
6160+
```csharp
6161+
record C([property: Attr1] int X, [field: Attr2] int Y);
6162+
```
6163+
61306164
#### §rec-class-pos-mem-props Properties
61316165

61326166
For each parameter of a positional *record_declaration* ([§15.2.1](classes.md#1521-general)) there shall be a corresponding public property member whose name and type are taken from the value parameter declaration.

0 commit comments

Comments
 (0)