From 748e0a6a600553f8269d0102b723b0489c3b2fa6 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 14:29:05 -0400 Subject: [PATCH 01/14] add support for primary constructors Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/expressions.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/standard/expressions.md b/standard/expressions.md index fc6f7f8ee..21030144e 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -1976,6 +1976,13 @@ In a member access of the form `E.I`, if `E` is a single identifier, and if the > > *end example* +With respect to primary constructors (§prim-constructor), the rule above affects whether an identifier within an instance member should be treated as a type reference, or as a primary constructor parameter reference, which, in turn, captures the parameter into the state of the enclosing type. Even though "the member lookup of `E.I` is never ambiguous," when lookup yields a member group, in some cases it is impossible to determine whether a member access refers to a static member or an instance member without fully resolving (binding) the member access. At the same time, capturing a primary constructor parameter changes properties of enclosing type in a way that affects semantic analysis. For example, the type might become unmanaged and fail certain constraints because of that. There are even scenarios for which binding can succeed either way, depending on whether the parameter is considered captured or not. + +An ambiguity error shall result for a member access `E.I` when all the following conditions are met: + +- Member lookup of `E.I` yields a member group containing instance and static members at the same time. Extension methods applicable to the receiver type are treated as instance methods for the purpose of this check. +- If `E` is treated as a simple name, rather than a type name, it would refer to a primary constructor parameter and would capture the parameter into the state of the enclosing type. + ### 12.8.8 Null Conditional Member Access A *null_conditional_member_access* is a conditional version of *member_access* ([§12.8.7](expressions.md#1287-member-access)) and it is a binding time error if the result type is `void`. For a null conditional expression where the result type may be `void` see ([§12.8.11](expressions.md#12811-null-conditional-invocation-expression)). From 0613270ff82893b4dda76454fdb990f88bb8bcb0 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 14:32:45 -0400 Subject: [PATCH 02/14] add support for primary constructors Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/arrays.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/standard/arrays.md b/standard/arrays.md index eabe31c19..2bca45436 100644 --- a/standard/arrays.md +++ b/standard/arrays.md @@ -311,3 +311,9 @@ When an array creation expression includes both explicit dimension lengths and a > *Note*: C# allows a trailing comma at the end of an *array_initializer*. This syntax provides flexibility in adding or deleting members from such a list, and simplifies machine generation of such lists. *end note* + +A warning shall be produced for a *variable_initializer* when all the following conditions are true: + +- The variable initializer represents an implicit or explicit identity conversion of a primary constructor parameter (§prim-constructor); +- The primary constructor parameter is captured into the state of the enclosing type. +- From a7c332d7d4e899a1bf4e5f36c498cd13eee11ec5 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 14:37:42 -0400 Subject: [PATCH 03/14] add support for primary constructors Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/interfaces.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/standard/interfaces.md b/standard/interfaces.md index 4bfea985a..12fdc4a27 100644 --- a/standard/interfaces.md +++ b/standard/interfaces.md @@ -19,11 +19,11 @@ An *interface_declaration* is a *type_declaration* ([§14.8](namespaces.md#148-t interface_declaration : attributes? interface_modifier* 'partial'? 'interface' identifier variant_type_parameter_list? interface_base? - type_parameter_constraints_clause* interface_body ';'? + type_parameter_constraints_clause* interface_body ; ``` -An *interface_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *interface_modifier*s ([§19.2.2](interfaces.md#1922-interface-modifiers)), followed by an optional partial modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `interface` and an *identifier* that names the interface, followed by an optional *variant_type_parameter_list* specification ([§19.2.3](interfaces.md#1923-variant-type-parameter-lists)), followed by an optional *interface_base* specification ([§19.2.4](interfaces.md#1924-base-interfaces)), followed by an optional *type_parameter_constraints_clause*s specification ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by an *interface_body* ([§19.3](interfaces.md#193-interface-body)), optionally followed by a semicolon. +An *interface_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *interface_modifier*s ([§19.2.2](interfaces.md#1922-interface-modifiers)), followed by an optional partial modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `interface` and an *identifier* that names the interface, followed by an optional *variant_type_parameter_list* specification ([§19.2.3](interfaces.md#1923-variant-type-parameter-lists)), followed by an optional *interface_base* specification ([§19.2.4](interfaces.md#1924-base-interfaces)), followed by an optional *type_parameter_constraints_clause*s specification ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by an *interface_body* ([§19.3](interfaces.md#193-interface-body)). An interface declaration shall not supply *type_parameter_constraints_clause*s unless it also supplies a *variant_type_parameter_list*. @@ -214,10 +214,13 @@ The *interface_body* of an interface defines the members of the interface. ```ANTLR interface_body - : '{' interface_member_declaration* '}' + : '{' interface_member_declaration* '}' ';'? + | ';' ; ``` +The *interface_body*s `{}`, `{};`, and `;` are equivalent, and the *interface_body*s `{…}` and `{…};` are equivalent. + ## 19.4 Interface members ### 19.4.1 General From e9ec760911ce61790d8c9ecab36a496eb444275d Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 14:42:44 -0400 Subject: [PATCH 04/14] add support for primary constructors Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/enums.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/standard/enums.md b/standard/enums.md index 4ce22401c..c6bd0f809 100644 --- a/standard/enums.md +++ b/standard/enums.md @@ -26,7 +26,7 @@ An enum declaration declares a new enum type. An enum declaration begins with th ```ANTLR enum_declaration - : attributes? enum_modifier* 'enum' identifier enum_base? enum_body ';'? + : attributes? enum_modifier* 'enum' identifier enum_base? enum_body ; enum_base @@ -39,13 +39,18 @@ integral_type_name ; enum_body - : '{' enum_member_declarations? '}' - | '{' enum_member_declarations ',' '}' + : '{' enum_member_declarations? '}' ';'? + | '{' enum_member_declarations ',' '}' ';'? + | ';' ; ``` +The *enum_body*s `{}`, `{};`, and `;` are equivalent, and the *enum_body*s `{…}` and `{…};` are equivalent. + Each enum type has a corresponding integral type called the ***underlying type*** of the enum type. This underlying type shall be able to represent all the enumerator values defined in the enumeration. If the *enum_base* is present, it explicitly declares the underlying type. The underlying type shall be one of the *integral types* ([§8.3.6](types.md#836-integral-types)) other than `nint`, `nuint`, and `char`. The underlying type may be specified either by an `integral_type` ([§8.3.5](types.md#835-simple-types)), or an `integral_type_name`. The `integral_type_name` is resolved in the same way as `type_name` ([§7.7.1](basic-concepts.md#771-general)), including taking any using directives ([§14.6](namespaces.md#146-using-directives)) into account. +> *Note*: The `char` type cannot be used as an underlying type, either by keyword or via an `integral_type_name`. *end note* + An enum declaration that does not explicitly declare an underlying type has an underlying type of `int`. > *Example*: The example From ff83ec6fa1b3144b0bd13cd3b36a1c713aa79301 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 15:02:54 -0400 Subject: [PATCH 05/14] add support for primary constructors Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/classes.md | 256 +++++++++++++++++++++++--------------------- 1 file changed, 136 insertions(+), 120 deletions(-) diff --git a/standard/classes.md b/standard/classes.md index 6a35f5a18..786755aa7 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -19,15 +19,30 @@ class_declaration ; non_record_class_declaration + : non_record_class_without_positional_members + | non_record_class_with_positional_members + ; + +non_record_class_without_positional_members + : attributes? class_modifier* 'partial'? 'class' identifier + type_parameter_list? class_base? + type_parameter_constraints_clause* class_body + ; + +non_record_class_with_positional_members : attributes? class_modifier* 'partial'? 'class' identifier - type_parameter_list? class_base? type_parameter_constraints_clause* - class_body + type_parameter_list? delimited_parameter_list class_base? + type_parameter_constraints_clause* class_body ; ``` -There are two kinds of class: ***non-record class***, as declared by *non_record_class_declaration*, and ***record class***, as declared by *record_class_declaration*. A non-record class is the kind of class that C# has supported since the language’s inception. Record classes were added much later and are discussed in [§15.16](classes.md#1516-record-classes). The differences between the two kinds are discussed in [§15.17](classes.md#1517-record-class-and-non-record-class-differences). +There are two kinds of class: ***non-record class***, as declared by *non_record_class_declaration*, and ***record class***, as declared by *record_class_declaration*. A non-record class is the kind of class that C# has supported since the language’s inception. Record classes were added much later and are discussed in [§15.16](classes.md#1516-record-classes). The differences between the two kinds are discussed in [§15.17](classes.md#1517-record-class-and-non-record-class-differences. + +A *non_record_class_declaration* can have one of two almost identical forms: *non_record_class_without_positional_members* and *non_record_class_with_positional_members*. -A *non_record_class_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *class_modifier*s ([§15.2.2](classes.md#1522-class-modifiers)), followed by an optional `partial` modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `class` and an *identifier* that names the class, followed by an optional *type_parameter_list* ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *class_base* specification ([§15.2.4](classes.md#1524-class-base-specification)), followed by an optional set of *type_parameter_constraints_clause*s ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *class_body* ([§15.2.6](classes.md#1526-class-body)). +A *non_record_class_without_positional_members* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *class_modifier*s ([§15.2.2](classes.md#1522-class-modifiers)), followed by an optional `partial` modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `class` and an *identifier* that names the class, followed by an optional *type_parameter_list* ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *class_base* specification ([§15.2.4](classes.md#1524-class-base-specification)), followed by an optional set of *type_parameter_constraints_clause*s ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *class_body* ([§15.2.6](classes.md#1526-class-body)). + +A *non_record_class_with_positional_members* has the same syntax but requires a *delimited_parameter_list*, as shown above in that grammar rule. For a discussion of *delimited_parameter_list*, see §prim-constructor. A class having a required member ([§15.7.1](classes.md#1571-general)) directly (that is, not through inheritance) shall be treated as if it were decorated with the attribute `System.Runtime.CompilerServices.RequiredMemberAttribute` ([§23.5.12.2](attributes.md#235122-the-requiredmember-attribute)). @@ -199,12 +214,22 @@ class_base | ':' class_type base_argument_list? ',' interface_type_list ; +base_argument_list + : '(' argument_list? ')' + ; + interface_type_list : interface_type (',' interface_type)* ; ``` -*base_argument_list* is discussed in [§15.16.2](classes.md#15162-class-base-specification). +*argument_list* corresponds to the base class’s positional member list *delimited_parameter_list*. + +A warning shall be produced for an in or by-value argument in a *base_argument_list* when all the following conditions are true: + +- The argument represents an implicit or explicit identity conversion of a primary constructor parameter (§prim-constructor); +- The argument is not part of an expanded params argument; +- The primary constructor parameter is captured into the state of the enclosing type. A record class may not inherit from a non-record class other than `object`, and a non-record class may not inherit from a record class. @@ -791,10 +816,11 @@ The *class_body* of a class defines the members of that class. ```ANTLR class_body : '{' class_member_declaration* '}' ';'? + | ';' ; ``` -The *class_body*s `{…}` and `{…};` are equivalent. +The *class_body*s `{}`, `{};`, and `;` are equivalent, and the *class_body*s `{…}` and `{…};` are equivalent. ### 15.2.7 Partial type declarations @@ -864,7 +890,7 @@ The handling of attributes specified on the type or type parameters of different ### 15.3.1 General -The members of a class consist of the members introduced by its *class_member_declaration*s and the members inherited from the direct base class. +The members of a class consist of the members introduced by its *class_member_declaration*s, the members inherited from the direct base class, and any members implicitly provided by the implementation ([§15.16.6](classes.md#15166-implicit-record-class-members)). ```ANTLR class_member_declaration @@ -5604,6 +5630,106 @@ If overload resolution is unable to determine a unique best candidate for the ba > > *end example* +### §prim-constructor Primary constructors + +For a class type with a *delimited_parameter_list* the implementation shall provide a public constructor whose signature corresponds to the value parameters, if any, of the type declaration. This constructor is called the ***primary constructor*** for that type, and causes the implicitly declared default constructor, to be suppressed. It is an error to have a primary constructor and an explicit constructor with the same signature in the type. If the type declaration does not include a *delimited_parameter_list*, no primary constructor is provided. + +Consider the following: + + +```csharp +public class Person(string FirstName, string LastName) +{ + public string? Title { get; set; } + public Person(string title, string fName, string lName) : this(fName, lName) + { + Title = title; + } + public override string ToString() + { + return (Title != null ? Title + " " : "") + FirstName + " " + LastName; + } +} + +class Program +{ + static void Main() + { + Console.WriteLine(new Person("Jane", "Wilson"); + Console.WriteLine(new Person("Dr.", "Jane", "Wilson"); + } +} +``` + +The output produced is: + +```console +Jane Wilson +Dr. Jane Wilson +``` + +Based on the class’s *delimited_parameter_list*, a primary constructor with the following signature is provided (the parameter names are for expository purposes only): + +```csharp +public Person(string firstName, string lastName); +``` + +As shown, the *constructor_initializer* of the explicit constructor is a call to the primary constructor, as is required by all user-defined constructors. + +At runtime the primary constructor + +1. Stores the value of each parameter in some unspecified manner. +1. Executes the instance initializers appearing in *class_body*. +1. Invokes the base record class constructor with the arguments provided in the *record_base* clause, if present. + +Each reference to a parameter in user code is replaced with a reference to the corresponding storage place. + +It is an error to reference a primary constructor parameter if the reference does not occur within one of the following: + +- a `nameof` argument. +- an initializer of an instance field, property or event of the declaring type. +- the `argument_list` of `class_base` of the declaring type. +- the body of an instance method of the declaring type. +- the body of an instance accessor of the declaring type. + +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: + +```csharp +class C(int i) +{ + protected int i = i; + public int I => i; +} +``` + +the initializer for the field `i` references the parameter `i`, whereas the body of the property `I` references the field `i`. + +A warning shall be produced if a parameter of the primary constructor is not read. + +Expression variables declared in *argument_list* are in scope within the *argument_list*. The same shadowing rules as within an argument list of a regular *constructor_initializer* apply. + +All instance member initializers in *class_body* become assignments in the primary constructor. + +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. + +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*: + +- The argument represents an implicit or explicit identity conversion of a primary constructor parameter; +- The argument is not part of an expanded `params` argument; + +If the class being declared has a *class_base* containing *base_argument_list*, the primary constructor shall have a *constructor_initializer* of the form `: base (` … `)` that corresponds to the *class_base*’s *delimited_parameter_list*, if any. + +A parameter in a *delimited_parameter_list* can be declared `ref`, `in`, or `out`. + +If a primary constructor parameter is referenced from within an instance member, and the reference is not a `nameof` argument, it shall be captured into the state of the enclosing type, so that it remains accessible after the termination of the constructor. Access to captured parameters within a readonly member have similar restrictions as access to instance fields in the same context. +Capturing is not permitted for a parameter that has ref-like type, and capturing is not permitted for ref, in, or out parameters. +A warning shall be issued for primary constructor parameters under the following circumstances: + +- For a by-value parameter, if the parameter is not captured and is not read within any instance initializers or base initializer. +- For an in parameter, if the parameter is not read within any instance initializers or base initializer. +- For a ref parameter, if the parameter is not read or written to within any instance initializers or base initializer. +A *class_declaration* may have a `method:` attribute target, which shall apply to the corresponding primary constructor, if any. If the class has no *delimited_parameter_list*, this attribute shall be ignored and a warning produced. + ## 15.12 Static constructors A ***static constructor*** is a member that implements the actions required to initialize a closed class. Static constructors are declared using *static_constructor_declaration*s: @@ -6306,7 +6432,7 @@ record_class_declaration ; ``` -A *record_class_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *class_modifier*s ([§15.2.2](classes.md#1522-class-modifiers)), followed by an optional `partial` modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `record`, optionally followed by the keyword `class`, and an *identifier* that names the class, followed by an optional *type_parameter_list* ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *delimited_parameter_list* ([§15.6.2.1](classes.md#15621-general)), followed by an optional *class_base* specification ([§15.2.4](classes.md#1524-class-base-specification)), followed by an optional set of *type_parameter_constraints_clause*s ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *record_class_body* ([§15.16.3](classes.md#15163-record-class-body)). +A *record_class_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *class_modifier*s ([§15.2.2](classes.md#1522-class-modifiers)), followed by an optional `partial` modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `record`, optionally followed by the keyword `class`, and an *identifier* that names the class, followed by an optional *type_parameter_list* ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *delimited_parameter_list* ([§15.6.2.1](classes.md#15621-general)), followed by an optional *class_base* specification ([§15.2.4](classes.md#1524-class-base-specification)), followed by an optional set of *type_parameter_constraints_clause*s ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *class_body* ([§15.2.6](classes.md#1526-class-body)). *class_modifier* shall not be `static`. @@ -6316,33 +6442,8 @@ At most only one partial type declaration of a partial record class may provide Parameters in *delimited_parameter_list* shall not have `ref`, `out` or `this` modifiers; however, `in` and `params` modifiers are permitted. -### 15.16.2 Class base specification - -```ANTLR -base_argument_list - : '(' argument_list? ')' - ; -``` - -*argument_list* corresponds to the base class’s positional member list *delimited_parameter_list*. - -### 15.16.3 Record class body - -The *record_class_body* of a record class identifies the explicitly declared members of that class. - -```ANTLR -record_class_body - : class_body - | ';' - ; -``` - -The *record_class_body*s `{}`, `{};`, and `;` are equivalent. They all indicate that the only members are those implicitly provided by the implementation ([§15.16.6](classes.md#15166-implicit-record-class-members)). - ### 15.16.4 Class members -For a record class, the member set also includes the members implicitly provided by the implementation ([§15.16.6](classes.md#15166-implicit-record-class-members)). - It is an error for a member of a record class to be named `Clone`. It is an error for an instance field of a record class to have an unsafe type. @@ -6355,7 +6456,7 @@ A positional record class ([§15.16.1](classes.md#15161-general)) has a primary #### 15.16.6.1 General -Certain members are provided by the implementation unless a member with a matching signature is declared in the *record_class_body*, or an accessible concrete, non-virtual member with a matching signature is inherited. A matching member prevents the implementation from providing that member only, not any other provided members. Two members are considered matching if they have the same signature or would be considered hiding in an inheritance scenario. +Certain members are provided by the implementation unless a member with a matching signature is declared in the *class_body*, or an accessible concrete, non-virtual member with a matching signature is inherited. A matching member prevents the implementation from providing that member only, not any other provided members. Two members are considered matching if they have the same signature or would be considered hiding in an inheritance scenario. The members provided by the implementation are described in the following subclauses. @@ -6778,92 +6879,7 @@ As well as providing the members described in the preceding subclauses, position ##### 15.16.6.6.2 Primary constructor -For a record class type with a *delimited_parameter_list* the implementation shall provide a public constructor whose signature corresponds to the value parameters, if any, of the type declaration. This constructor is called the ***primary constructor*** for that type, and causes the implicitly declared default constructor, to be suppressed. It is an error to have a primary constructor and an explicit constructor with the same signature in the type. If the type declaration does not include a *delimited_parameter_list*, no primary constructor is provided. - -Consider the following: - - -```csharp -public record Person(string FirstName, string LastName) -{ - public string? Title { get; set; } - public Person(string title, string fName, string lName) : this(fName, lName) - { - Title = title; - } - public override string ToString() - { - return (Title != null ? Title + " " : "") + FirstName + " " + LastName; - } -} - -class Program -{ - static void Main() - { - Console.WriteLine(new Person("Jane", "Wilson")); - Console.WriteLine(new Person("Dr.", "Jane", "Wilson")); - } -} -``` - -The output produced is: - -```console -Jane Wilson -Dr. Jane Wilson -``` - -Based on the class’s *delimited_parameter_list*, a primary constructor with the following signature is provided (the parameter names are for expository purposes only): - -```csharp -public Person(string firstName, string lastName); -``` - -As shown, the *constructor_initializer* of the explicit constructor is a call to the primary constructor, as is required by all user-defined constructors. - -At runtime the primary constructor - -1. Stores the value of each parameter in the corresponding provided private field (see [§15.16.6.6.3](classes.md#1516663-properties)). -1. Executes the instance initializers appearing in *record_class_body*. -1. Invokes the base record class constructor with the arguments provided in the *record_base* clause, if present. - -Each reference to a parameter in user code is replaced with a reference to the corresponding provided field. - -It is an error to reference a primary constructor parameter if the reference does not occur within one of the following: - -- a `nameof` argument. -- an initializer of an instance field, property or event of the declaring type. -- the `argument_list` of `class_base` of the declaring type. -- the body of an instance method of the declaring type. -- the body of an instance accessor of the declaring type. - -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: - -```csharp -record class C(int i) -{ - protected int i = i; - public int I => i; -} -``` - -the initializer for the field `i` references the parameter `i`, whereas the body of the property `I` references the field `i`. - -A warning shall be produced if a parameter of the primary constructor is not read. - -Expression variables declared in *argument_list* are in scope within the *argument_list*. The same shadowing rules as within an argument list of a regular *constructor_initializer* apply. - -All instance member initializers in *record_class_body* become assignments in the primary constructor. - -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. - -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*: - -- The argument represents an implicit or explicit identity conversion of a primary constructor parameter; -- The argument is not part of an expanded `params` argument; - -If the class being declared has a *class_base* containing *base_argument_list*, the primary constructor shall have a *constructor_initializer* of the form `: base (` … `)` that corresponds to the *class_base*’s *delimited_parameter_list*, if any. +The primary constructor of a record class is like that of a non-record class (§prim-constructor), with the following difference: Each parameter value is stored in a corresponding private instance field having a corresponding property with set and get accessors. ##### 15.16.6.6.3 Properties From e9c28cc62a896ce7a405d3e950900a88857ca016 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 15:15:00 -0400 Subject: [PATCH 06/14] add support for primary constructors Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/structs.md | 62 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/standard/structs.md b/standard/structs.md index e0c7fb5f5..870462195 100644 --- a/standard/structs.md +++ b/standard/structs.md @@ -21,9 +21,20 @@ struct_declaration ; non_record_struct_declaration + : non_record_struct_without_positional_members + | non_record_struct_with_positional_members + ; + +non_record_struct_without_positional_members : attributes? struct_modifier* 'ref'? 'partial'? 'struct' identifier type_parameter_list? struct_interfaces? - type_parameter_constraints_clause* struct_body ';'? + type_parameter_constraints_clause* struct_body + ; + +non_record_struct_with_positional_members + : attributes? struct_modifier* 'ref'? 'partial'? 'struct' + identifier type_parameter_list? delimited_parameter_list struct_interfaces? + type_parameter_constraints_clause* struct_body ; record_struct_declaration @@ -40,7 +51,11 @@ record_struct_body A ***struct*** is declared by *non_record_struct_declaration*, and a ***record struct*** is declared by *record_struct_declaration*. Record structs are discussed in [§16.4](structs.md#164-record-structs). The differences between the two kinds are discussed in [§16.5](structs.md#165-record-struct-and-struct-differences). -A *non_record_struct_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *struct_modifier*s ([§16.2.2](structs.md#1622-struct-modifiers)), followed by an optional `ref` modifier ([§16.2.3](structs.md#1623-ref-modifier)), followed by an optional partial modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `struct` and an *identifier* that names the struct, followed by an optional *type_parameter_list* specification ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *struct_interfaces* specification ([§16.2.5](structs.md#1625-struct-interfaces)), followed by an optional *type_parameter_constraints-clauses* specification ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *struct_body* ([§16.2.6](structs.md#1626-struct-body)), optionally followed by a semicolon. +A *non_record_struct_declaration* can have one of two almost identical forms: *non_record_struct_without_positional_members* and *non_record_struct_with_positional_members*. + +A *non_record_struct_without_positional_members* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *struct_modifier*s ([§16.2.2](structs.md#1622-struct-modifiers)), followed by an optional `ref` modifier ([§16.2.3](structs.md#1623-ref-modifier)), followed by an optional partial modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `struct` and an *identifier* that names the struct, followed by an optional *type_parameter_list* specification ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *struct_interfaces* specification ([§16.2.5](structs.md#1625-struct-interfaces)), followed by an optional *type_parameter_constraints-clauses* specification ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *struct_body* ([§16.2.6](structs.md#1626-struct-body)), optionally followed by a semicolon. + +A *non_record_struct_with_positional_members* has the same syntax but requires a *delimited_parameter_list*, as shown above in that grammar rule. For a discussion of *delimited_parameter_list*, see §prim-constructor. A struct having a required member ([§15.7.1](classes.md#1571-general)) directly (that is, not through inheritance) shall be treated as if it were decorated with the attribute `System.Runtime.CompilerServices.RequiredMemberAttribute` ([§23.5.12.2](attributes.md#235122-the-requiredmember-attribute)). A *struct_declaration* shall not supply *type_parameter_constraints_clause*s unless it also supplies a *type_parameter_list*. @@ -132,15 +147,18 @@ The *struct_body* of a struct defines the members of the struct. ```ANTLR struct_body - : '{' struct_member_declaration* '}' + : '{' struct_member_declaration* '}' ';'? + | ';' ; ``` +The *struct_body*s `{}`, `{};`, and `;` are equivalent, and the *struct_body*s `{…}` and `{…};` are equivalent. + ## 16.3 Struct members ### 16.3.1 General -The members of a struct consist of the members introduced by its *struct_member_declaration*s and the members inherited from the type `System.ValueType`. +The members of a struct consist of the members introduced by its *struct_member_declaration*s, the members inherited from the type `System.ValueType``, and any members implicitly provided by the implementation ([§16.4.4](structs.md#1644-implicit-record-struct-members)). ```ANTLR struct_member_declaration @@ -206,13 +224,29 @@ An instance member definition or accessor of an instance property, indexer, or e > > The `readonly` method `AddMessage` can change the state of a message list. The `InitializeMessages` member can clear and re-initialize the list of messages. In the case of `AddMessage`, the `readonly` modifier is valid. In the case of `InitializeMessages`, adding the `readonly` modifier is invalid. *end example* +## §struct-prim-constructors Primary constructors + +As with a non-record class, a non-record struct with a *delimited_parameter_list* has a primary constructor (§prim-constructor) provided by the implementation. The semantics of the non-record class version apply here as well and are augmented by the text in this subclause. + +In the case of a non-record class, the implementation shall provide a private, init-only field for each parameter. However, for a non-record struct, the storage is read-write and provided in some unspecified manner. + +Instance field declarations for a non-record struct are permitted to include variable initializers. If there is no primary constructor, the instance initializers execute as part of the parameterless constructor. Otherwise, at runtime the primary constructor executes the instance initializers appearing in the *struct_body*. + ## 16.4 Record structs ### 16.4.1 General A record struct is a specialized value type that is optimized for storing data rather than behavior. It provides built-in functionality that would normally require significant “boilerplate” code in a struct, such as value-based equality and easy immutability. -A *record_struct_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *struct_modifier*s ([§16.2.2](structs.md#1622-struct-modifiers)), followed by an optional partial modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `record`, followed by the keyword `struct` and an *identifier* that names the struct, followed by an optional *type_parameter_list* specification ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *delimited_parameter_list* specification ([§15.6.2.1](classes.md#15621-general)), followed by an optional *struct_interfaces* specification ([§16.2.5](structs.md#1625-struct-interfaces)), followed by an optional *type_parameter_constraints-clauses* specification ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *record_struct_body*. +```ANTLR +record_struct_declaration + : attributes? struct_modifier* 'partial'? 'record' 'struct' + identifier type_parameter_list? delimited_parameter_list? struct_interfaces? + type_parameter_constraints_clause* struct_body + ; +``` + +A *record_struct_declaration* consists of an optional set of *attributes* ([§23](attributes.md#23-attributes)), followed by an optional set of *struct_modifier*s ([§16.2.2](structs.md#1622-struct-modifiers)), followed by an optional partial modifier ([§15.2.7](classes.md#1527-partial-type-declarations)), followed by the keyword `record`, followed by the keyword `struct` and an *identifier* that names the struct, followed by an optional *type_parameter_list* specification ([§15.2.3](classes.md#1523-type-parameters)), followed by an optional *delimited_parameter_list* specification ([§15.6.2.1](classes.md#15621-general)), followed by an optional *struct_interfaces* specification ([§16.2.5](structs.md#1625-struct-interfaces)), followed by an optional *type_parameter_constraints-clauses* specification ([§15.2.5](classes.md#1525-type-parameter-constraints)), followed by a *struct_body* ([§16.2.6](structs.md#1626-struct-body)). A *record_struct_declaration* having a *delimited_parameter_list* declares a ***positional record struct***. @@ -222,27 +256,25 @@ The parameters in *delimited_parameter_list* shall not have `ref`, `out` or `thi ### 16.4.2 Struct members -For a record struct, as well as those members identified by [§16.3](structs.md#163-struct-members), the member set includes the members implicitly provided by the implementation ([§16.4.4](structs.md#1644-implicit-record-struct-members)). - It is an error for a member of a record struct to be named `Clone`. It is an error for an instance field of a record struct to have an unsafe type. -### 16.4.3 Record struct body - -The *record_struct_body* of a record struct identifies the explicitly declared members of that struct. - -For a *record_struct_declaration*, the *record_struct_body*s `{}`, `{};`, and `;` are equivalent. They all indicate that the only members are those implicitly provided by the implementation ([§16.4.4](structs.md#1644-implicit-record-struct-members)). - ### 16.4.4 Implicit record struct members #### 16.4.4.1 General -In the case of a record struct, members are provided by the implementation unless a member with a “matching” signature is declared in the *record_struct_body* or an accessible concrete non-virtual member with a “matching” signature is inherited. A matching member prevents the implementation from providing that member only, not any other provided members. Two members are considered matching if they have the same signature or would be considered “hiding” in an inheritance scenario. (See Signatures and overloading [§7.5](basic-concepts.md#75-signatures-and-overloading).) +In the case of a record struct, members are provided by the implementation unless a member with a “matching” signature is declared in the *struct_body* or an accessible concrete non-virtual member with a “matching” signature is inherited. A matching member prevents the implementation from providing that member only, not any other provided members. Two members are considered matching if they have the same signature or would be considered “hiding” in an inheritance scenario. (See Signatures and overloading [§7.5](basic-concepts.md#75-signatures-and-overloading).) The members provided by the implementation are described in the following subclauses. -#### 16.4.4.2 Equality members +#### 16.4.4.2 Primary constructors + +The primary constructor of a record struct is like that of a non-record struct (§struct-prim-constructors), with the following difference: Each parameter value is stored in a corresponding private instance field having a corresponding property with set and get accessors. + +Instance field declarations for a non-record struct are permitted to include variable initializers. If there is no primary constructor, the instance initializers execute as part of the parameterless constructor. Otherwise, at runtime the primary constructor executes the instance initializers appearing in the *struct_body*. + +#### 16.4.4.3 Equality members The provided equality members are similar to those for a record class ([§15.16.6.3](classes.md#151663-equality-members)), except for the lack of method `EqualityContract`, null checks, or inheritance. From a142f0ec114db8e82a7f3807b809c9642b2cb132 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 15:23:25 -0400 Subject: [PATCH 07/14] fix md Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/arrays.md | 1 - 1 file changed, 1 deletion(-) diff --git a/standard/arrays.md b/standard/arrays.md index 2bca45436..8e7f0e70a 100644 --- a/standard/arrays.md +++ b/standard/arrays.md @@ -316,4 +316,3 @@ A warning shall be produced for a *variable_initializer* when all the following - The variable initializer represents an implicit or explicit identity conversion of a primary constructor parameter (§prim-constructor); - The primary constructor parameter is captured into the state of the enclosing type. -- From fe74fb1a2a17943f3e23b620ac6cecabc527ae32 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 15:25:25 -0400 Subject: [PATCH 08/14] fix md Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/classes.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/standard/classes.md b/standard/classes.md index 786755aa7..41d52ddc5 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -5669,7 +5669,7 @@ Dr. Jane Wilson ``` Based on the class’s *delimited_parameter_list*, a primary constructor with the following signature is provided (the parameter names are for expository purposes only): - + ```csharp public Person(string firstName, string lastName); ``` @@ -5719,7 +5719,7 @@ A primary constructor parameter is considered to be passed to the base type via If the class being declared has a *class_base* containing *base_argument_list*, the primary constructor shall have a *constructor_initializer* of the form `: base (` … `)` that corresponds to the *class_base*’s *delimited_parameter_list*, if any. -A parameter in a *delimited_parameter_list* can be declared `ref`, `in`, or `out`. +A parameter in a *delimited_parameter_list* can be declared `ref`, `in`, or `out`. If a primary constructor parameter is referenced from within an instance member, and the reference is not a `nameof` argument, it shall be captured into the state of the enclosing type, so that it remains accessible after the termination of the constructor. Access to captured parameters within a readonly member have similar restrictions as access to instance fields in the same context. Capturing is not permitted for a parameter that has ref-like type, and capturing is not permitted for ref, in, or out parameters. From 5910d807b3677d99c528b575c0bea1e100151264 Mon Sep 17 00:00:00 2001 From: Rex Jaeschke Date: Sat, 8 Aug 2026 15:26:35 -0400 Subject: [PATCH 09/14] fix md Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/expressions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/standard/expressions.md b/standard/expressions.md index 21030144e..33fb3e67a 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -1976,7 +1976,7 @@ In a member access of the form `E.I`, if `E` is a single identifier, and if the > > *end example* -With respect to primary constructors (§prim-constructor), the rule above affects whether an identifier within an instance member should be treated as a type reference, or as a primary constructor parameter reference, which, in turn, captures the parameter into the state of the enclosing type. Even though "the member lookup of `E.I` is never ambiguous," when lookup yields a member group, in some cases it is impossible to determine whether a member access refers to a static member or an instance member without fully resolving (binding) the member access. At the same time, capturing a primary constructor parameter changes properties of enclosing type in a way that affects semantic analysis. For example, the type might become unmanaged and fail certain constraints because of that. There are even scenarios for which binding can succeed either way, depending on whether the parameter is considered captured or not. +With respect to primary constructors (§prim-constructor), the rule above affects whether an identifier within an instance member should be treated as a type reference, or as a primary constructor parameter reference, which, in turn, captures the parameter into the state of the enclosing type. Even though "the member lookup of `E.I` is never ambiguous," when lookup yields a member group, in some cases it is impossible to determine whether a member access refers to a static member or an instance member without fully resolving (binding) the member access. At the same time, capturing a primary constructor parameter changes properties of enclosing type in a way that affects semantic analysis. For example, the type might become unmanaged and fail certain constraints because of that. There are even scenarios for which binding can succeed either way, depending on whether the parameter is considered captured or not. An ambiguity error shall result for a member access `E.I` when all the following conditions are met: From 83203a07e6f2e743715b17721f37906baf697839 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 21 Sep 2026 17:10:28 -0400 Subject: [PATCH 10/14] Fix primary constructor example syntax Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bce5e82a-89fd-4655-bc08-6d6ba97f0cc6 Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/classes.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/standard/classes.md b/standard/classes.md index 41d52ddc5..c0af96d02 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -5655,8 +5655,8 @@ class Program { static void Main() { - Console.WriteLine(new Person("Jane", "Wilson"); - Console.WriteLine(new Person("Dr.", "Jane", "Wilson"); + Console.WriteLine(new Person("Jane", "Wilson")); + Console.WriteLine(new Person("Dr.", "Jane", "Wilson")); } } ``` From 22432997b43beb05fe67b2a5a02a85843660c9e5 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 21 Sep 2026 17:22:38 -0400 Subject: [PATCH 11/14] Fix primary constructor section link Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bce5e82a-89fd-4655-bc08-6d6ba97f0cc6 Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/standard/classes.md b/standard/classes.md index c0af96d02..f2df6c199 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -36,7 +36,7 @@ non_record_class_with_positional_members ; ``` -There are two kinds of class: ***non-record class***, as declared by *non_record_class_declaration*, and ***record class***, as declared by *record_class_declaration*. A non-record class is the kind of class that C# has supported since the language’s inception. Record classes were added much later and are discussed in [§15.16](classes.md#1516-record-classes). The differences between the two kinds are discussed in [§15.17](classes.md#1517-record-class-and-non-record-class-differences. +There are two kinds of class: ***non-record class***, as declared by *non_record_class_declaration*, and ***record class***, as declared by *record_class_declaration*. A non-record class is the kind of class that C# has supported since the language’s inception. Record classes were added much later and are discussed in [§15.16](classes.md#1516-record-classes). The differences between the two kinds are discussed in [§15.17](classes.md#1517-record-class-and-non-record-class-differences). A *non_record_class_declaration* can have one of two almost identical forms: *non_record_class_without_positional_members* and *non_record_class_with_positional_members*. From 7f9ce902fc094f3ceccdc70f40183a2c3b03be89 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 14 Oct 2026 14:30:38 -0400 Subject: [PATCH 12/14] Remove stale record grammar left behind by the C12 struct-body relocation Normative Base build found duplicate record_struct_declaration. Preserve the feature-authored record-struct production in its dedicated clause and the C12 generalized struct_body; remove the obsolete earlier production and its unused record_struct_body wrapper. No CSharp example, annotation, prose or tooling change; no inherited-fatal waiver used for this defect. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/structs.md | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/standard/structs.md b/standard/structs.md index 870462195..f7fefaaa3 100644 --- a/standard/structs.md +++ b/standard/structs.md @@ -36,17 +36,6 @@ non_record_struct_with_positional_members identifier type_parameter_list? delimited_parameter_list struct_interfaces? type_parameter_constraints_clause* struct_body ; - -record_struct_declaration - : attributes? struct_modifier* 'partial'? 'record' 'struct' - identifier type_parameter_list? delimited_parameter_list? struct_interfaces? - type_parameter_constraints_clause* record_struct_body - ; - -record_struct_body - : struct_body ';'? - | ';' - ; ``` A ***struct*** is declared by *non_record_struct_declaration*, and a ***record struct*** is declared by *record_struct_declaration*. Record structs are discussed in [§16.4](structs.md#164-record-structs). The differences between the two kinds are discussed in [§16.5](structs.md#165-record-struct-and-struct-differences). From f0785f4a468104bea2b093d5de827dbd41e44e61 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 14 Oct 2026 14:44:30 -0400 Subject: [PATCH 13/14] Complete C12 record-body references after primary-constructor body generalization Authoritative #1766 adds the bare-semicolon alternative to class_body and removes record_class_body, whose approved predecessor alternatives were class_body or semicolon. Therefore new class_body is exactly the existing record-body language, also explicitly selected by feature prose. Redirect the dangling production to this existing rule, without adding aliases or removing alternatives. Align the residual record-struct initializer prose with the analogous feature-owned struct_body generalization. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e --- standard/basic-concepts.md | 4 ++-- standard/classes.md | 2 +- standard/structs.md | 12 +++--------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/standard/basic-concepts.md b/standard/basic-concepts.md index cd4802a91..705443743 100644 --- a/standard/basic-concepts.md +++ b/standard/basic-concepts.md @@ -686,12 +686,12 @@ The ***scope*** of a name is the region of program text within which it is possi - The scope of a name defined or imported by a *using_directive* ([§14.6](namespaces.md#146-using-directives)) extends over the *global_attributes*, *statement_list*, and *namespace_member_declaration*s of the *compilation_unit* or *namespace_body* in which the *using_directive* occurs. A *using_directive* may make zero or more namespace or type names available within a particular *compilation_unit* or *namespace_body*, but does not contribute any new members to the underlying declaration space. In other words, a *using_directive* is not transitive but rather affects only the *compilation_unit* or *namespace_body* in which it occurs. - The scope of a type parameter declared by a *type_parameter_list* on a *class_declaration* ([§15.2](classes.md#152-class-declarations)) is the *class_base*, *type_parameter_constraints_clause*s, and *class_body* (or *record_class_body*) of that *class_declaration*. > *Note*: Unlike members of a class, this scope does not extend to derived classes. *end note* -- The scope of a type parameter declared by a *type_parameter_list* on a *struct_declaration* ([§16.2](structs.md#162-struct-declarations)) is the *delimited_parameter_list* (if any), *struct_interfaces*, *type_parameter_constraints_clause*s, and *struct_body* (or *record_struct_body*) of that *struct_declaration*. +- The scope of a type parameter declared by a *type_parameter_list* on a *struct_declaration* ([§16.2](structs.md#162-struct-declarations)) is the *delimited_parameter_list* (if any), *struct_interfaces*, *type_parameter_constraints_clause*s, and *struct_body* of that *struct_declaration*. - The scope of a type parameter declared by a *type_parameter_list* on an *interface_declaration* ([§19.2](interfaces.md#192-interface-declarations)) is the *interface_base*, *type_parameter_constraints_clause*s, and *interface_body* of that *interface_declaration*. - The scope of a type parameter declared by a *type_parameter_list* on a *delegate_declaration* ([§21.2](delegates.md#212-delegate-declarations)) is the *return_type*, *parameter_list*, and *type_parameter_constraints_clause*s of that *delegate_declaration*. - The scope of a type parameter declared by a *type_parameter_list* on a *method_declaration* ([§15.6.1](classes.md#1561-general)) is the *method_declaration* and `nameof` expressions in an attribute on the *method_declaration* or its parameters. - The scope of a member declared by a *class_member_declaration* ([§15.3.1](classes.md#1531-general)) is the *class_body* (or *record_class_body*) in which the declaration occurs. In addition, the scope of a class member extends to the *class_body* (or *record_class_body*) of those derived classes that are included in the accessibility domain ([§7.4.3](basic-concepts.md#743-accessibility-domains)) of the member. -- The scope of a member declared by a *struct_member_declaration* ([§16.3](structs.md#163-struct-members)) is the *struct_body* (or *record_struct_body*) in which the declaration occurs. +- The scope of a member declared by a *struct_member_declaration* ([§16.3](structs.md#163-struct-members)) is the *struct_body* in which the declaration occurs. - The scope of a member declared by an *enum_member_declaration* ([§20.4](enums.md#204-enum-members)) is the *enum_body* in which the declaration occurs. - The scope of a parameter declared in a *method_declaration* ([§15.6](classes.md#156-methods)) is the *method_body* or *ref_method_body* of that *method_declaration* and `nameof` expressions in an attribute on the method declaration or its parameters. - The scope of a parameter declared in an *indexer_declaration* ([§15.9](classes.md#159-indexers)) is the *indexer_body* of that *indexer_declaration*. diff --git a/standard/classes.md b/standard/classes.md index f2df6c199..3821c2b2d 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -6428,7 +6428,7 @@ A record class is a specialized reference type that is optimized for storing dat record_class_declaration : attributes? class_modifier* 'partial'? 'record' 'class'? identifier type_parameter_list? delimited_parameter_list? class_base? - type_parameter_constraints_clause* record_class_body + type_parameter_constraints_clause* class_body ; ``` diff --git a/standard/structs.md b/standard/structs.md index f7fefaaa3..3751107a3 100644 --- a/standard/structs.md +++ b/standard/structs.md @@ -257,13 +257,7 @@ In the case of a record struct, members are provided by the implementation unles The members provided by the implementation are described in the following subclauses. -#### 16.4.4.2 Primary constructors - -The primary constructor of a record struct is like that of a non-record struct (§struct-prim-constructors), with the following difference: Each parameter value is stored in a corresponding private instance field having a corresponding property with set and get accessors. - -Instance field declarations for a non-record struct are permitted to include variable initializers. If there is no primary constructor, the instance initializers execute as part of the parameterless constructor. Otherwise, at runtime the primary constructor executes the instance initializers appearing in the *struct_body*. - -#### 16.4.4.3 Equality members +#### 16.4.4.2 Equality members The provided equality members are similar to those for a record class ([§15.16.6.3](classes.md#151663-equality-members)), except for the lack of method `EqualityContract`, null checks, or inheritance. @@ -442,9 +436,9 @@ A record struct with a *delimited_parameter_list* has a public ***primary constr If a record struct has a primary constructor, every explicitly declared instance constructor shall have a `this` constructor initializer ([§15.11.2](classes.md#15112-constructor-initializers)) that invokes the primary constructor or another explicitly declared instance constructor. The rules for resolving that initializer and for circular constructor dependencies apply as for other struct constructors ([§16.6.9](structs.md#1669-constructors)). -Primary constructor parameters are in scope throughout the *record_struct_body*. Within an initializer of an instance field, property, or event, a primary constructor parameter shadows a member with the same name and may be used as a value. Elsewhere in that body, members shadow primary constructor parameters, and a primary constructor parameter shall not be referenced except within a `nameof` argument. No additional storage is provided merely because a primary constructor parameter is declared. Properties and their backing fields are provided only as specified in [§16.4.4.4.3](structs.md#164443-properties). A warning may be produced if a primary constructor parameter is not read. +Primary constructor parameters are in scope throughout the *struct_body*. Within an initializer of an instance field, property, or event, a primary constructor parameter shadows a member with the same name and may be used as a value. Elsewhere in that body, members shadow primary constructor parameters, and a primary constructor parameter shall not be referenced except within a `nameof` argument. No additional storage is provided merely because a primary constructor parameter is declared. Properties and their backing fields are provided only as specified in [§16.4.4.4.3](structs.md#164443-properties). A warning may be produced if a primary constructor parameter is not read. -At run time, the primary constructor initializes the backing fields of the auto-properties provided by [§16.4.4.4.3](structs.md#164443-properties) to the values of the corresponding parameters, then executes the instance initializers in the *record_struct_body* in textual order ([§15.5.6.1](classes.md#15561-general)). A matching explicitly declared field or property is not automatically initialized from the corresponding parameter; an initializer or constructor assignment is required to store that value. +At run time, the primary constructor initializes the backing fields of the auto-properties provided by [§16.4.4.4.3](structs.md#164443-properties) to the values of the corresponding parameters, then executes the instance initializers in the *struct_body* in textual order ([§15.5.6.1](classes.md#15561-general)). A matching explicitly declared field or property is not automatically initialized from the corresponding parameter; an initializer or constructor assignment is required to store that value. Instance field declarations in a record struct may include variable initializers. In the absence of a primary constructor, explicitly declared instance constructors execute instance initializers according to the struct constructor rules ([§16.6.9](structs.md#1669-constructors)). Implicit default initialization ([§16.6.5](structs.md#1665-default-values)) does not execute instance initializers. From e09eb2c6cc5b9d7cf8e10a41c3e4caf57d41853a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 25 Nov 2026 17:09:15 -0500 Subject: [PATCH 14/14] Clarify primary constructor rules A few bits of terminology were missing, and some rules on `out` parameters needed clarification. --- standard/classes.md | 42 ++++++++++++++++++++++-------------------- standard/structs.md | 6 ++---- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/standard/classes.md b/standard/classes.md index 3821c2b2d..429e5acca 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -223,9 +223,9 @@ interface_type_list ; ``` -*argument_list* corresponds to the base class’s positional member list *delimited_parameter_list*. +The *argument_list* in a *base_argument_list* supplies arguments to a constructor of the direct base class, selected as specified in [§15.11.2](classes.md#15112-constructor-initializers). The base class need not have a primary constructor. -A warning shall be produced for an in or by-value argument in a *base_argument_list* when all the following conditions are true: +A warning may be produced for an `in` or by-value argument in a *base_argument_list* when all the following conditions are true: - The argument represents an implicit or explicit identity conversion of a primary constructor parameter (§prim-constructor); - The argument is not part of an expanded params argument; @@ -828,6 +828,8 @@ The modifier `partial` is used when defining a class, struct, or interface type Each part of a ***partial type*** declaration shall include a `partial` modifier and shall be declared in the same namespace or containing type as the other parts. The `partial` modifier indicates that additional parts of the type declaration might exist elsewhere, but the existence of such additional parts is not a requirement; it is valid for the only declaration of a type to include the `partial` modifier. It is valid for only one declaration of a partial type to include the base class or implemented interfaces. However, all declarations of a base class or implemented interfaces shall match, including the nullability of any specified type arguments. +At most one partial type declaration of a partial class or struct may provide a *delimited_parameter_list*, including an empty list. This rule applies to all types, including `record` types, regardless of the parameter lists’ signatures. + For a partial type, either all parts shall include the `file` modifier and be declared in the same compilation unit, or no part shall include the `file` modifier. All parts of a partial type shall be compiled together such that the parts can be merged at compile-time. Partial types specifically do not allow already compiled types to be extended. @@ -5632,7 +5634,7 @@ If overload resolution is unable to determine a unique best candidate for the ba ### §prim-constructor Primary constructors -For a class type with a *delimited_parameter_list* the implementation shall provide a public constructor whose signature corresponds to the value parameters, if any, of the type declaration. This constructor is called the ***primary constructor*** for that type, and causes the implicitly declared default constructor, to be suppressed. It is an error to have a primary constructor and an explicit constructor with the same signature in the type. If the type declaration does not include a *delimited_parameter_list*, no primary constructor is provided. +For a class type with a *delimited_parameter_list* the implementation shall provide a public constructor with the parameters of that list. Its signature is determined as for an explicitly declared instance constructor ([§15.11.1](classes.md#15111-general)), including the parameter types and parameter-passing modes. This constructor is called the ***primary constructor*** for that type, and causes the implicitly declared default constructor to be suppressed. It is an error to have a primary constructor and an explicit constructor with the same signature in the type. If the type declaration does not include a *delimited_parameter_list*, no primary constructor is provided. Consider the following: @@ -5674,15 +5676,15 @@ Based on the class’s *delimited_parameter_list*, a primary constructor with th public Person(string firstName, string lastName); ``` -As shown, the *constructor_initializer* of the explicit constructor is a call to the primary constructor, as is required by all user-defined constructors. +As shown, the *constructor_initializer* of the explicit constructor is a call to the primary constructor. Every explicitly declared instance constructor shall have a `this` constructor initializer that invokes the primary constructor or another explicitly declared instance constructor, so that the primary constructor is ultimately invoked. The rules for resolving constructor initializers and for circular constructor dependencies ([§15.11.2](classes.md#15112-constructor-initializers)) apply. The copy constructor of a record class is an exception ([§15.16.6.2](classes.md#151662-copy-constructors)). At runtime the primary constructor -1. Stores the value of each parameter in some unspecified manner. -1. Executes the instance initializers appearing in *class_body*. -1. Invokes the base record class constructor with the arguments provided in the *record_base* clause, if present. +1. Initializes the retained storage for captured parameters, if any, to the corresponding parameter values. +1. Executes the instance initializers appearing in *class_body*, in the order specified in [§15.11.3](classes.md#15113-instance-variable-initializers). +1. Invokes the direct base class constructor as specified in [§15.11.2](classes.md#15112-constructor-initializers), using the arguments in the *base_argument_list* of *class_base*, if present, and otherwise an implicit `base()` initializer. -Each reference to a parameter in user code is replaced with a reference to the corresponding storage place. +References to a captured parameter access its retained storage. A parameter used only during initialization does not require retained instance storage; such uses access the constructor parameter instead. It is an error to reference a primary constructor parameter if the reference does not occur within one of the following: @@ -5704,30 +5706,32 @@ class C(int i) the initializer for the field `i` references the parameter `i`, whereas the body of the property `I` references the field `i`. -A warning shall be produced if a parameter of the primary constructor is not read. - Expression variables declared in *argument_list* are in scope within the *argument_list*. The same shadowing rules as within an argument list of a regular *constructor_initializer* apply. All instance member initializers in *class_body* become assignments in the primary constructor. -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. +A warning may 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. 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*: - The argument represents an implicit or explicit identity conversion of a primary constructor parameter; - The argument is not part of an expanded `params` argument; -If the class being declared has a *class_base* containing *base_argument_list*, the primary constructor shall have a *constructor_initializer* of the form `: base (` … `)` that corresponds to the *class_base*’s *delimited_parameter_list*, if any. +If the class being declared has a *class_base* containing a *base_argument_list*, the primary constructor shall have a *constructor_initializer* of the form `: base (` … `)` with the same *argument_list*, if any, as that *base_argument_list*. A parameter in a *delimited_parameter_list* can be declared `ref`, `in`, or `out`. -If a primary constructor parameter is referenced from within an instance member, and the reference is not a `nameof` argument, it shall be captured into the state of the enclosing type, so that it remains accessible after the termination of the constructor. Access to captured parameters within a readonly member have similar restrictions as access to instance fields in the same context. -Capturing is not permitted for a parameter that has ref-like type, and capturing is not permitted for ref, in, or out parameters. -A warning shall be issued for primary constructor parameters under the following circumstances: +If a primary constructor parameter is referenced from the body of an instance method or instance accessor, and the reference is not within a `nameof` argument, it shall be captured into the state of the enclosing type, so that it remains accessible after the termination of the constructor. Such capture requires retained storage in an unspecified manner; neither a particular name nor a particular layout is required. Captured parameters are mutable, except that in a readonly struct they have the same restrictions as readonly instance fields. Within a readonly struct member, access to captured parameters has the same restrictions as access to instance fields in that context. Uses in instance member initializers or base constructor arguments do not by themselves require capture into the state of the enclosing type. + +Capturing is not permitted for a parameter that has ref-like type, and capturing is not permitted for `ref`, `in`, or `out` parameters. +A warning may be issued for primary constructor parameters under the following circumstances: - For a by-value parameter, if the parameter is not captured and is not read within any instance initializers or base initializer. -- For an in parameter, if the parameter is not read within any instance initializers or base initializer. -- For a ref parameter, if the parameter is not read or written to within any instance initializers or base initializer. +- For an `in` parameter, if the parameter is not read within any instance initializers or base initializer. +- For a `ref` parameter, if the parameter is neither read nor written to within any instance initializers or base initializer. + +An `out` parameter shall be definitely assigned as required for an explicitly declared instance constructor ([§9.2.7](variables.md#927-output-parameters)). + A *class_declaration* may have a `method:` attribute target, which shall apply to the corresponding primary constructor, if any. If the class has no *delimited_parameter_list*, this attribute shall be ignored and a warning produced. ## 15.12 Static constructors @@ -6438,8 +6442,6 @@ A *record_class_declaration* consists of an optional set of *attributes* ([§23] A *record_class_declaration* having a *delimited_parameter_list* declares a ***positional record class***. -At most only one partial type declaration of a partial record class may provide a *delimited_parameter_list*. - Parameters in *delimited_parameter_list* shall not have `ref`, `out` or `this` modifiers; however, `in` and `params` modifiers are permitted. ### 15.16.4 Class members @@ -6879,7 +6881,7 @@ As well as providing the members described in the preceding subclauses, position ##### 15.16.6.6.2 Primary constructor -The primary constructor of a record class is like that of a non-record class (§prim-constructor), with the following difference: Each parameter value is stored in a corresponding private instance field having a corresponding property with set and get accessors. +The primary constructor of a record class follows §prim-constructor, with the additional member synthesis specified in [§15.16.6.6.3](classes.md#1516663-properties). Any provided auto-property has `get` and `init` accessors and is initialized to the corresponding parameter value; matching explicitly declared or inherited members prevent synthesis as specified in [§15.16.6.1](classes.md#151661-general) and [§15.16.6.6.3](classes.md#1516663-properties). ##### 15.16.6.6.3 Properties diff --git a/standard/structs.md b/standard/structs.md index 3751107a3..f7dda1e8a 100644 --- a/standard/structs.md +++ b/standard/structs.md @@ -217,9 +217,9 @@ An instance member definition or accessor of an instance property, indexer, or e As with a non-record class, a non-record struct with a *delimited_parameter_list* has a primary constructor (§prim-constructor) provided by the implementation. The semantics of the non-record class version apply here as well and are augmented by the text in this subclause. -In the case of a non-record class, the implementation shall provide a private, init-only field for each parameter. However, for a non-record struct, the storage is read-write and provided in some unspecified manner. +Capture and access to captured parameters follow §prim-constructor, including the restrictions for readonly structs and readonly members. A primary constructor parameter does not by itself cause a property to be provided. -Instance field declarations for a non-record struct are permitted to include variable initializers. If there is no primary constructor, the instance initializers execute as part of the parameterless constructor. Otherwise, at runtime the primary constructor executes the instance initializers appearing in the *struct_body*. +Instance field declarations for a non-record struct are permitted to include variable initializers. Instance constructors execute these initializers according to [§16.6.8.1](structs.md#16681-field-initializers) and [§16.6.9](structs.md#1669-constructors), whether or not the struct has a primary constructor. A primary constructor initializes any retained storage for captured parameters and then executes the instance initializers in the *struct_body* in textual order ([§15.5.6.1](classes.md#15561-general)); it does not invoke a base class constructor. Every other explicitly declared instance constructor shall chain to the primary constructor as specified in §prim-constructor. Execution of the implicitly provided default parameterless constructor and default initialization ([§16.6.5](structs.md#1665-default-values)), including default initialization of array elements ([§17.3](arrays.md#173-array-creation)), do not execute instance initializers. ## 16.4 Record structs @@ -239,8 +239,6 @@ A *record_struct_declaration* consists of an optional set of *attributes* ([§23 A *record_struct_declaration* having a *delimited_parameter_list* declares a ***positional record struct***. -At most only one *record_struct_declaration* containing `partial` may provide a *delimited_parameter_list*. - The parameters in *delimited_parameter_list* shall not have `ref`, `out` or `this` modifiers; however, `in` and `params` modifiers are permitted. ### 16.4.2 Struct members