You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: standard/classes.md
+56-21Lines changed: 56 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5720,19 +5720,48 @@ It is an error for an instance field of a record class to have an unsafe type.
5720
5720
5721
5721
A positional record class (§rec-class-general) has a synthesized primary constructor; see §rec-class-pos-mem-pricon for more information.
5722
5722
5723
+
### §synth-members Synthesized record class members
5723
5724
5725
+
#### §synth-members-general General
5724
5726
5727
+
Certain members are synthesized by the compiler 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 compiler from generating that member only, not any other synthesized members. Two members are considered matching if they have the same signature or would be considered hiding in an inheritance scenario.
5725
5728
5729
+
The synthesized members are described in the following subclauses.
5726
5730
5727
-
##§synth-members Synthesized record class members
5731
+
#### §copy-constructor Copy constructors
5728
5732
5729
-
### §synth-members-general General
5733
+
A ***copy constructor*** for a type `T` is a constructor having a single parameter of type `T`. The purpose of a copy constructor is to copy the state from the parameter to the new instance being created.
5730
5734
5731
-
Certain members are synthesized by the compiler 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 compiler from generating that member only, not any other synthesized members. Two members are considered matching if they have the same signature or would be considered hiding in an inheritance scenario.
>therecordclassis immutable. The synthesized auto properties `Age` and `Name` are read-init. A copy constructor is synthesized, as is a primary constructor. *end example*
5732
5761
5733
-
The synthesized members are described in the following subclauses.
5762
+
In certain circumstances (§rec-class-copyclone), a copy constructor may be synthesized by the compiler, and called by synthesized code.
5734
5763
5735
-
### §rec-class-equalmem Equality members
5764
+
#### §rec-class-equalmem Equality members
5736
5765
5737
5766
If a record class is derived directly from `object`, the record class type has a synthesized, readonly property equivalent to a property declared as follows:
5738
5767
@@ -5752,7 +5781,7 @@ The property may be declared explicitly. It is an error if the explicit declarat
5752
5781
5753
5782
The record class type implements `System.IEquatable<R>` and includes a synthesized, strongly-typed overload of `Equals(R? other)` where `R` is the record class type. The method is `public`, and the method is `virtual` unless the record class type is `sealed`. The method can be declared explicitly. It is an error if the explicit declaration does not match the expected signature or accessibility, or the explicit declaration doesn't allow overriding in a derived type and the record class type is not `sealed`.
5754
5783
5755
-
If `Equals(R? other)` is user-defined (that is, not synthesized) but `GetHashCode` is not, a warning shall be issued.
5784
+
If `Equals(R? other)` is user-defined but `GetHashCode` is not, a warning shall be issued.
5756
5785
5757
5786
```csharp
5758
5787
publicvirtualboolEquals(R?other);
@@ -5899,7 +5928,7 @@ The synthesized override of `GetHashCode()` returns an `int` result of combining
5899
5928
>
5900
5929
>*endexample*
5901
5930
5902
-
### §rec-class-copyclone Copy and clone members
5931
+
#### §rec-class-copyclone Copy and clone members
5903
5932
5904
5933
Arecordclasstype contains two copying members:
5905
5934
@@ -5910,7 +5939,7 @@ The copy constructor shall not execute any instance field/property initializers
### §rec-class-pos-mem Positional record class members
6110
+
#### §rec-class-pos-mem Positional record class members
6082
6111
6083
-
#### §rec-class-pos-mem-gen General
6112
+
##### §rec-class-pos-mem-gen General
6084
6113
6085
6114
Aswellasprovidingthemembersdescribedintheprecedingsubclauses, positionalrecordclasses ([§15.2.1](classes.md#1521-general)) synthesize additional members with the same conditions as the other members, as described in the following subclauses.
A record class type shall have a public constructor whose signature corresponds to the value parameters of the type declaration. This is called the ***primary constructor*** for the type, and causes the implicitly declared default constructor, if present, to be suppressed. It is an error to have a primary constructor and a constructor with the same signature already present in the class.
6118
+
A record class type shall have a public constructor whose signature corresponds to the value parameters of the type declaration, if any. This is called the ***primary constructor*** for the type, and causes the implicitly declared default constructor, if present, to be suppressed. It is an error to have a primary constructor and a constructor with the same signature already present in the class.
6090
6119
6091
6120
At runtime the primary constructor
6092
6121
6093
6122
1. Stores the value of each parameter in the corresponding generated field.
6094
-
1. Executes the instance initializers appearing in *record_body*.
6123
+
1. Executes the instance initializers appearing in *record_class_body*.
6095
6124
1. Invokes the base record class constructor with the arguments provided in the *record_base* clause, if present.
6096
6125
6097
6126
Each reference to a parameter in user code is replaced with a reference to the corresponding generated field.
@@ -6160,13 +6189,7 @@ A primary constructor parameter is considered to be passed to the base type via
Apositional record class ([§15.2.1](classes.md#1521-general)) with at least one parameter causes to be synthesized a public `void`-returning instance method called `Deconstruct` with an out parameter declaration for each parameter of the primary constructor declaration. Each parameter of `Deconstruct` has the same type as the corresponding parameter of the primary constructor declaration. The body of the method assigns to each parameter of `Deconstruct` the value from an instance member access to a member of the same name. The method may be declared explicitly. It is an error if the explicit declaration does not match the expected signature or accessibility, or is static.
6200
6223
@@ -6223,3 +6246,15 @@ A positional record class ([§15.2.1](classes.md#1521-general)) with at least on
6223
6246
> ```
6224
6247
>
6225
6248
>*endexample*
6249
+
6250
+
## §rec-class-diffs Record class and non-record class differences
6251
+
6252
+
Arecordclassdiffers from a non-record class in several important ways:
0 commit comments