diff --git a/standard/basic-concepts.md b/standard/basic-concepts.md index 2fd15b6a2..69ff90ae8 100644 --- a/standard/basic-concepts.md +++ b/standard/basic-concepts.md @@ -683,7 +683,7 @@ The ***scope*** of a name is the region of program text within which it is possi - The scope of a namespace member declared by a *namespace_member_declaration* within a *namespace_declaration* whose fully qualified name is `N`, is the *namespace_body* of every *namespace_declaration* whose fully qualified name is `N` or starts with `N`, followed by a period. - The scope of a name defined by an *extern_alias_directive* ([§14.4](namespaces.md#144-extern-alias-directives)) extends over the *using_directive*s, *global_attributes*, *statement_list*, and *namespace_member_declaration*s of its immediately containing *compilation_unit* or *namespace_body*. An *extern_alias_directive* does not contribute any new members to the underlying declaration space. In other words, an *extern_alias_directive* is not transitive, but, rather, affects only the *compilation_unit* or *namespace_body* in which it occurs. - The scope of a name defined or imported by a *using_directive* ([§14.5](namespaces.md#145-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*. +- The scope of a type parameter declared by a *type_parameter_list* in a class declaration ([§15.2](classes.md#152-class-declarations) and §rec-class-general) is the base specification, *type_parameter_constraints_clause*s, and body of that declaration, and, for a record class, its *delimited_parameter_list*. > *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 *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*. diff --git a/standard/classes.md b/standard/classes.md index 83fbc497d..1c5b2b32b 100644 --- a/standard/classes.md +++ b/standard/classes.md @@ -14,20 +14,18 @@ A *class_declaration* is a *type_declaration* ([§14.7](namespaces.md#147-type-d ```ANTLR class_declaration - : non_record_class_declaration - | record_class_declaration - ; - -non_record_class_declaration : attributes? class_modifier* 'partial'? 'class' identifier type_parameter_list? class_base? type_parameter_constraints_clause* - class_body + class_body ';'? + | attributes? class_modifier* 'partial'? 'record' identifier + type_parameter_list? delimited_parameter_list? record_class_base? + type_parameter_constraints_clause* record_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 §rec-class. The differences between the two kinds are discussed in §rec-class-diffs. +Record classes are discussed in §rec-class. -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 *class_declaration* using `class` 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)) and an optional semicolon. A declaration using `record` is described in §rec-class-general. A class declaration shall not supply *type_parameter_constraints_clause*s unless it also supplies a *type_parameter_list*. @@ -175,13 +173,13 @@ Two partial generic type declarations (in the same program) contribute to the sa #### 15.2.4.1 General -A class declaration may include a *class_base* specification, which defines the direct base class of the class and the interfaces ([§19](interfaces.md#19-interfaces)) directly implemented by the class. +A class declaration may include a *class_base* specification, or a *record_class_base* specification (§rec-class-class-base-specification) when declared using `record`, which defines the direct base class of the class and the interfaces ([§19](interfaces.md#19-interfaces)) directly implemented by the class. The rules of this subclause apply to both specifications. ```ANTLR class_base - : ':' class_type base_argument_list? + : ':' class_type | ':' interface_type_list - | ':' class_type base_argument_list? ',' interface_type_list + | ':' class_type ',' interface_type_list ; interface_type_list @@ -189,15 +187,11 @@ interface_type_list ; ``` -*base_argument_list* is discussed in §rec-class-class-base-specification. - -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. - -It is an error for *class_base* to have a *base_argument_list* if the corresponding *class_declaration* does not contain a *delimited_parameter_list*. - #### 15.2.4.2 Base classes -When a *class_type* is included in the *class_base*, it specifies the direct base class of the class being declared. If a non-partial class declaration has no *class_base*, or if the *class_base* lists only interface types, the direct base class is assumed to be `object`. When a partial class declaration includes a base class specification, that base class specification shall reference the same type as all other parts of that partial type that include a base class specification. If no part of a partial class includes a base class specification, the base class is `object`. A class inherits members from its direct base class, as described in [§15.3.4](classes.md#1534-inheritance). +When a *class_type* is included in the *class_base* or *record_class_base*, it specifies the direct base class of the class being declared. If a non-partial class declaration has no base specification, or if the base specification lists only interface types, the direct base class is assumed to be `object`. When a partial class declaration includes a base class specification, that base class specification shall reference the same type as all other parts of that partial type that include a base class specification. If no part of a partial class includes a base class specification, the base class is `object`. A class inherits members from its direct base class, as described in [§15.3.4](classes.md#1534-inheritance). + +The direct base class of a record class shall be `object` or another record class. A class whose direct base class is a record class shall itself be a record class. > *Example*: In the following code > @@ -349,7 +343,7 @@ It is not possible to derive from a sealed class. #### 15.2.4.3 Interface implementations -A *class_base* specification may include a list of interface types, in which case the class is said to implement the given interface types. For a constructed class type, including a nested type declared within a generic type declaration ([§15.3.9.7](classes.md#15397-nested-types-in-generic-classes)), each implemented interface type is obtained by substituting, for each *type_parameter* in the given interface, the corresponding *type_argument* of the constructed type. +A *class_base* or *record_class_base* specification may include a list of interface types, in which case the class is said to implement the given interface types. For a constructed class type, including a nested type declared within a generic type declaration ([§15.3.9.7](classes.md#15397-nested-types-in-generic-classes)), each implemented interface type is obtained by substituting, for each *type_parameter* in the given interface, the corresponding *type_argument* of the constructed type. The set of interfaces for a type declared in multiple parts ([§15.2.7](classes.md#1527-partial-type-declarations)) is the union of the interfaces specified on each part. A particular interface can only be named once on each part, but multiple parts can name the same base interfaces. There shall only be one implementation of each member of any given interface. @@ -764,11 +758,11 @@ The *class_body* of a class defines the members of that class. ```ANTLR class_body - : '{' class_member_declaration* '}' ';'? + : '{' class_member_declaration* '}' ; ``` -The *class_body*s `{…}` and `{…};` are equivalent. +The optional semicolon following a *class_body* in a class declaration has no effect. ### 15.2.7 Partial type declarations @@ -4904,7 +4898,7 @@ When a constructor declaration includes an `extern` modifier, the constructor is A *constructor_body* that is a *block* or expression body corresponds exactly to the *block* of an instance method with a `void` return type ([§15.6.11](classes.md#15611-method-body)). -Instance constructors are not inherited. Thus, a class has no instance constructors other than those actually declared in the class, with the exception that if a class contains no instance constructor declarations, a default instance constructor is automatically provided ([§15.11.5](classes.md#15115-default-constructors)). +Instance constructors are not inherited. A class has the instance constructors declared in the class and those provided by the implementation. Default instance constructors are described in [§15.11.5](classes.md#15115-default-constructors); primary and copy constructors for record classes are described in §rec-class. Instance constructors are invoked by *object_creation_expression*s ([§12.8.17.2](expressions.md#128172-object-creation-expressions)) and through *constructor_initializer*s. @@ -4913,7 +4907,7 @@ Instance constructors are invoked by *object_creation_expression*s ([§12.8.17.2 All instance constructors (except those for class `object`) implicitly include an invocation of another instance constructor immediately before the *constructor_body*. The constructor to implicitly invoke is determined by the *constructor_initializer*: - An instance constructor initializer of the form `base(`*argument_list*`)` (where *argument_list* is optional) causes an instance constructor from the direct base class to be invoked. That constructor is selected using *argument_list* and the overload resolution rules of [§12.6.4](expressions.md#1264-overload-resolution). The set of candidate instance constructors consists of all the accessible instance constructors of the direct base class. If this set is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. -- An instance constructor initializer of the form `this(`*argument_list*`)` (where *argument_list* is optional) invokes another instance constructor from the same class. The constructor is selected using *argument_list* and the overload resolution rules of [§12.6.4](expressions.md#1264-overload-resolution). The set of candidate instance constructors consists of all instance constructors declared in the class itself. If the resulting set of applicable instance constructors is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. If an instance constructor declaration invokes itself through a chain of one or more constructor initializers, a compile-time error occurs. +- An instance constructor initializer of the form `this(`*argument_list*`)` (where *argument_list* is optional) invokes another instance constructor from the same class. The constructor is selected using *argument_list* and the overload resolution rules of [§12.6.4](expressions.md#1264-overload-resolution). The set of candidate instance constructors consists of all instance constructors of the class, including those provided by the implementation. If the resulting set of applicable instance constructors is empty, or if a single best instance constructor cannot be identified, a compile-time error occurs. If an instance constructor declaration invokes itself through a chain of one or more constructor initializers, a compile-time error occurs. If an instance constructor has no constructor initializer, a constructor initializer of the form `base()` is implicitly provided. @@ -5082,7 +5076,7 @@ Variable initializers are transformed into assignment statements, and these assi ### 15.11.5 Default constructors -If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes a constructor of the direct base class, as if it had a constructor initializer of the form `base()`. If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. +If a class contains no instance constructor declarations, a default instance constructor is automatically provided, subject to the provisions for record class constructors (§rec-class-copyclone and §rec-class-pos-mem-pricon). That default constructor simply invokes a constructor of the direct base class, as if it had a constructor initializer of the form `base()`. If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. > *Note*: Thus, the default constructor is always of the form > @@ -5671,35 +5665,74 @@ An asynchronous enumerable object provides an implementation of the `GetAsyncEnu ### §rec-class-general General -A record class is a specialized reference type that is optimized for storing data rather than behavior. It provides built-in functionality that would normally require significant “boilerplate” code in a non-record class, such as value-based equality and easy immutability. - -```ANTLR -record_class_declaration - : attributes? class_modifier* 'partial'? 'record' identifier - type_parameter_list? delimited_parameter_list? class_base? - type_parameter_constraints_clause* record_class_body - ; -``` +A ***record class*** is a class declared using `record`. The rules for classes apply, subject to the provisions of this subclause. Record classes provide built-in functionality for storing data, such as value-based equality and support for immutability. -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` 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* (§rec-class-record-class-body). +A *class_declaration* using `record` 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` 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 *record_class_base* specification (§rec-class-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* (§rec-class-record-class-body). *class_modifier* shall not be `static`. -A *record_class_declaration* having a *delimited_parameter_list* declares a ***positional record class***. +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*. +All declarations contributing to a partial record class shall use the keyword `record`. At most one of those declarations may provide a *delimited_parameter_list*. + +> *Example*: Each of the following declarations is valid. In particular, the type parameter `T` is in scope in the parameter list of `Derived`, and only one declaration of `Part` supplies a parameter list. +> +> +> ```csharp +> class C {} +> class D {}; +> record R1 {} +> record R2 {}; +> record R3; +> record R4() {} +> record R5() {}; +> record R6(); +> record Base(T Value); +> record Derived(T Value) : Base(Value); +> partial record Part(int Value); +> partial record Part {} +> ``` +> +> The following declarations violate the rules for partial record classes: +> +> +> ```csharp +> partial record Mixed; +> partial class Mixed {} +> partial record Lists(int X); +> partial record Lists(int X); +> ``` +> +> *end example* Parameters in *delimited_parameter_list* shall not have `ref`, `out` or `this` modifiers; however, `in` and `params` modifiers are permitted. ### §rec-class-class-base-specification Class base specification ```ANTLR +record_class_base + : ':' class_type base_argument_list? + | ':' interface_type_list + | ':' class_type base_argument_list? ',' interface_type_list + ; + base_argument_list : '(' argument_list? ')' ; ``` -*argument_list* corresponds to the base class’s positional member list *delimited_parameter_list*. +It is a compile-time error for a *record_class_base* to contain a *base_argument_list* unless the same declaration contains a *delimited_parameter_list*. The arguments select a constructor of the direct base class as specified in §rec-class-pos-mem-pricon. + +> *Example*: A parameter list in another partial declaration does not permit base arguments in this declaration: +> +> +> ```csharp +> record Base; +> partial record Derived(); +> partial record Derived : Base() {} +> ``` +> +> *end example* ### §rec-class-record-class-body Record class body @@ -5707,7 +5740,7 @@ The *record_class_body* of a record class identifies the explicitly declared mem ```ANTLR record_class_body - : class_body + : class_body ';'? | ';' ; ``` @@ -5754,7 +5787,7 @@ A ***copy constructor*** for a type `T` is a constructor having a single paramet > } > ```` > -> This declares a mutable, non-record class with two read-write properties, and a user-written copy constructor. +> This declares a mutable class using `class`, with two read-write properties and a user-written copy constructor. > > In the following case, > @@ -6169,7 +6202,7 @@ At runtime the primary constructor 1. Stores the value of each parameter in the corresponding provided private field (see §rec-class-pos-mem-props). 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. +1. Invokes the direct base class constructor with the arguments provided in the *record_class_base* clause, if present. Each reference to a parameter in user code is replaced with a reference to the corresponding provided field. @@ -6177,14 +6210,14 @@ It is an error to reference a primary constructor parameter if the reference doe - 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 *argument_list* of the *record_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: +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 the *record_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) +record C(int i) { protected int i = i; public int I => i; @@ -6201,12 +6234,12 @@ All instance member initializers in *record_class_body* become assignments in th 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*: +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 *record_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 record class being declared has a *record_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*. Otherwise, the primary constructor implicitly invokes the parameterless constructor of the direct base class. Constructor selection is performed as specified in [§15.11.2](classes.md#15112-constructor-initializers). ##### §rec-class-pos-mem-props Properties @@ -6267,15 +6300,3 @@ A positional record class ([§15.2.1](classes.md#1521-general)) with at least on > ``` > > *end example* - -## §rec-class-diffs Record class and non-record class differences - -A record class differs from a non-record class in several important ways: - -- It is declared using the keyword `record` instead of `class`. -- It has a number of members provided for it by the implementation, including a copy constructor. -- Its declaration may contain a *delimited_parameter_list* having zero or more parameters, which results in the provision of a primary constructor having those parameters. For each parameter, the implementation shall provide field-like storage and a property with get and init accessor. A `Deconstruct` method is also provided. -- If it is derived from other than `object`, it may pass arguments to its base type. -- Its class body may be omitted. -- It shall not have a member called `Clone`. -- It shall not have an instance field with an unsafe type. diff --git a/standard/expressions.md b/standard/expressions.md index 202f68d0f..3de36ff55 100644 --- a/standard/expressions.md +++ b/standard/expressions.md @@ -1561,7 +1561,7 @@ A *simple_name* is either of the form `I` or of the form `I`, - If `e` is zero and the *simple_name* appears within a local variable declaration space ([§7.2](basic-concepts.md#72-declarations)) that directly contains a local variable, parameter (with the exception of discard parameters ([§12.21.2](expressions.md#12212-anonymous-function-signatures))), or constant with name `I`, then the *simple_name* refers to that local variable, parameter or constant and is classified as a variable or value. - If `e` is zero and the *simple_name* appears within a generic method declaration but outside the *attributes* of its *method_declaration*, and if that declaration includes a type parameter with name `I`, then the *simple_name* refers to that type parameter. - Otherwise, for each instance type `T` ([§15.3.2](classes.md#1532-the-instance-type)), starting with the instance type of the immediately enclosing type declaration and continuing with the instance type of each enclosing class or struct declaration (if any): - - If the declaration of `T` includes a primary constructor parameter `I` and the reference occurs within the `argument_list` of `T`’s `class_base` or within an initializer of a field, property or event of `T`, the result is the primary constructor parameter `I`. + - If the declaration of `T` includes a primary constructor parameter `I` and the reference occurs within the *argument_list* of `T`’s *record_class_base* or within an initializer of a field, property or event of `T`, the result is the primary constructor parameter `I`. - Otherwise, if `e` is zero and the declaration of `T` includes a type parameter with name `I`, then the *simple_name* refers to that type parameter. - Otherwise, if a member lookup ([§12.5](expressions.md#125-member-lookup)) of `I` in `T` with `e` type arguments produces a match: - If `T` is the instance type of the immediately enclosing class or struct type and the lookup identifies one or more methods, the result is a method group with an associated instance expression of `this`. If a type argument list was specified, it is used in calling a generic method ([§12.8.10.2](expressions.md#128102-method-invocations)). diff --git a/standard/interfaces.md b/standard/interfaces.md index 819bc0572..46813760c 100644 --- a/standard/interfaces.md +++ b/standard/interfaces.md @@ -176,7 +176,7 @@ The ***base interface***s of an interface are the explicit base interfaces and t > > *end example* -Members inherited from a constructed generic type are inherited after type substitution. That is, any constituent types in the member have the base class declaration’s type parameters replaced with the corresponding type arguments used in the *class_base* specification. +Members inherited from a constructed generic type are inherited after type substitution. That is, any constituent types in the member have the base class declaration’s type parameters replaced with the corresponding type arguments used in the *class_base* or *record_class_base* specification. > *Example*: In the following code > diff --git a/standard/types.md b/standard/types.md index e8d94aa84..8498c6d1f 100644 --- a/standard/types.md +++ b/standard/types.md @@ -719,11 +719,29 @@ Whenever a constructed type or generic method is referenced, the supplied type a - `A` is a value type, since all value types have a public default constructor ([§8.3.3](types.md#833-default-constructors)). - `A` is a type parameter having the constructor constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)). - `A` is a type parameter having the value type constraint ([§15.2.5](classes.md#1525-type-parameter-constraints)). - - `A` is a `class` that is not abstract and contains an explicitly declared public constructor with no parameters. - - `A` is not `abstract` and has a default constructor ([§15.11.5](classes.md#15115-default-constructors)). + - `A` is a class that is not abstract and has a public parameterless instance constructor, whether explicitly declared or provided by the implementation ([§15.11.5](classes.md#15115-default-constructors) and §rec-class-pos-mem-pricon). A compile-time error occurs if one or more of a type parameter’s constraints are not satisfied by the given type arguments. +> *Example*: Both a default constructor and a parameterless primary constructor satisfy a constructor constraint: +> +> +> ```csharp +> public record Default; +> public record Primary(); +> public class Factory where T : class, new() +> { +> public static T Create() => new T(); +> } +> public class Test +> { +> public object A() => Factory.Create(); +> public object B() => Factory.Create(); +> } +> ``` +> +> *end example* + Since type parameters are not inherited, constraints are never inherited either. > *Example*: In the following, `D` needs to specify the constraint on its type parameter `T` so that `T` satisfies the constraint imposed by the base `class` `B`. In contrast, `class` `E` need not specify a constraint, because `List` implements `IEnumerable` for any `T`. @@ -817,7 +835,7 @@ The type `dynamic` is considered identical to `object` except in the following r - Type inference ([§12.6.3](expressions.md#1263-type-inference)) will prefer `dynamic` over `object` if both are candidates. - `dynamic` cannot be used as - the type in an *object_creation_expression* ([§12.8.17.2](expressions.md#128172-object-creation-expressions)) - - a *class_base* ([§15.2.4](classes.md#1524-class-base-specification)) + - a *class_base* ([§15.2.4](classes.md#1524-class-base-specification)) or *record_class_base* (§rec-class-class-base-specification) - a *predefined_type* in a *member_access* ([§12.8.7.1](expressions.md#12871-general)) - the operand of the `typeof` operator - an attribute argument diff --git a/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.gruntree.red.txt b/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.gruntree.red.txt index 83b4defa3..289458b2a 100644 --- a/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.gruntree.red.txt +++ b/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.gruntree.red.txt @@ -262,7 +262,7 @@ ⎜ ⎜ ⎛ ⎜ ⎜ ⎜ namespace_member_declaration ⎜ ⎜ ⎜ ⎛ -⎜ ⎜ ⎜ ⎜ non_record_class_declaration +⎜ ⎜ ⎜ ⎜ class_declaration ⎜ ⎜ ⎜ ⎜ partial ⎜ ⎜ ⎜ ⎜ class ⎜ ⎜ ⎜ ⎜ ⎛ diff --git a/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.tree.red.txt b/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.tree.red.txt index 20199a1be..b736c50ed 100644 --- a/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.tree.red.txt +++ b/tools/GrammarTesting/Tests/Parsing/Samples/v9/Top-level statements/Reference/sample.tree.red.txt @@ -1 +1 @@ -(prog (compilation_unit (using_directive (using_namespace_directive using (namespace_name (identifier System)) ;)) (using_directive (using_namespace_directive using (namespace_name (namespace_or_type_name (identifier System) . (identifier Runtime) . (identifier CompilerServices))) ;)) (statement_list (statement (expression_statement (statement_expression (invocation_expression (primary_expression (member_access (primary_expression (identifier Console)) . (identifier WriteLine))) ( (argument_list (interpolated_regular_string_expression 〔$"〕 { (regular_interpolation (invocation_expression (primary_expression (identifier GetName)) ( ))) } 〔: args[0] = 〕 { (regular_interpolation (element_access (primary_expression (identifier args)) [ (argument_list (literal 0)) ])) } 〔"〕)) ))) ;)) (statement (expression_statement (statement_expression (invocation_expression (primary_expression (member_access (primary_expression (identifier Program)) . (identifier Main))) ( (argument_list (identifier args)) ))) ;)) (statement (local_function_declaration (return_type (class_type string)) (local_function_header (identifier GetName) ( (parameter_list (fixed_parameter (attributes (attribute_section [ (attribute_list (identifier CallerMemberName)) ])) (type (class_type string)) (identifier memberName) (default_argument = (expression (literal ""))))) )) (local_function_body (block { (statement_list (return_statement return (expression (identifier memberName)) ;)) }))))) (namespace_member_declaration (non_record_class_declaration partial class (identifier Program) (class_body { (class_member_declaration (method_declaration (method_modifiers (method_modifier (ref_method_modifier public)) (method_modifier (ref_method_modifier static))) (return_type void) (method_header (member_name (identifier Main)) ( (parameter_list (fixed_parameter (type (array_type (non_array_type (class_type string)) (rank_specifier [ ]))) (identifier args))) )) (method_body (block { (statement_list (expression_statement (statement_expression (invocation_expression (primary_expression (member_access (primary_expression (identifier Console)) . (identifier WriteLine))) ( (argument_list (interpolated_regular_string_expression 〔$"〕 〔Main: args[0] = 〕 { (regular_interpolation (element_access (primary_expression (identifier args)) [ (argument_list (literal 0)) ])) } 〔"〕)) ))) ;)) })))) }))))) +(prog (compilation_unit (using_directive (using_namespace_directive using (namespace_name (identifier System)) ;)) (using_directive (using_namespace_directive using (namespace_name (namespace_or_type_name (identifier System) . (identifier Runtime) . (identifier CompilerServices))) ;)) (statement_list (statement (expression_statement (statement_expression (invocation_expression (primary_expression (member_access (primary_expression (identifier Console)) . (identifier WriteLine))) ( (argument_list (interpolated_regular_string_expression 〔$"〕 { (regular_interpolation (invocation_expression (primary_expression (identifier GetName)) ( ))) } 〔: args[0] = 〕 { (regular_interpolation (element_access (primary_expression (identifier args)) [ (argument_list (literal 0)) ])) } 〔"〕)) ))) ;)) (statement (expression_statement (statement_expression (invocation_expression (primary_expression (member_access (primary_expression (identifier Program)) . (identifier Main))) ( (argument_list (identifier args)) ))) ;)) (statement (local_function_declaration (return_type (class_type string)) (local_function_header (identifier GetName) ( (parameter_list (fixed_parameter (attributes (attribute_section [ (attribute_list (identifier CallerMemberName)) ])) (type (class_type string)) (identifier memberName) (default_argument = (expression (literal ""))))) )) (local_function_body (block { (statement_list (return_statement return (expression (identifier memberName)) ;)) }))))) (namespace_member_declaration (class_declaration partial class (identifier Program) (class_body { (class_member_declaration (method_declaration (method_modifiers (method_modifier (ref_method_modifier public)) (method_modifier (ref_method_modifier static))) (return_type void) (method_header (member_name (identifier Main)) ( (parameter_list (fixed_parameter (type (array_type (non_array_type (class_type string)) (rank_specifier [ ]))) (identifier args))) )) (method_body (block { (statement_list (expression_statement (statement_expression (invocation_expression (primary_expression (member_access (primary_expression (identifier Console)) . (identifier WriteLine))) ( (argument_list (interpolated_regular_string_expression 〔$"〕 〔Main: args[0] = 〕 { (regular_interpolation (element_access (primary_expression (identifier args)) [ (argument_list (literal 0)) ])) } 〔"〕)) ))) ;)) })))) })))))