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/structs.md
+66-25Lines changed: 66 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,25 +38,16 @@ record_struct_body
38
38
;
39
39
```
40
40
41
-
A *struct_declaration* is for either a***non-record struct*** or a ***record struct***.
41
+
There are two kinds of struct:***non-record struct***, as declared by *non_record_struct_declaration*, and ***record struct***, as declared by *record_struct_declaration*. A non-record struct is the kind of struct that C# has supported since the language’s inception. Record structs were added much later and are discussed in §record-structs. The differences between the two kinds are discussed in §record-struct-diffs.
42
42
43
43
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.
44
44
45
-
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.2.1](classes.md#1521-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*.
46
-
47
45
A *struct_declaration* shall not supply *type_parameter_constraints_clause*s unless it also supplies a *type_parameter_list*.
48
46
49
47
A *struct_declaration* that supplies a *type_parameter_list* is a generic struct declaration. Additionally, any struct nested inside a generic class declaration or a generic struct declaration is itself a generic struct declaration, since type arguments for the containing type shall be supplied to create a constructed type ([§8.4](types.md#84-constructed-types)).
50
48
51
49
A *non_record_struct_declaration* that includes a `ref` modifier shall not have a *struct_interfaces* part.
52
50
53
-
A *record_struct_declaration* having a *delimited_parameter_list* declares a ***positional record struct***.
54
-
55
-
At most only one *record_struct_declaration* containing `partial` may provide a *delimited_parameter_list*.
56
-
57
-
The parameters in *delimited_parameter_list* shall not have `ref`, `out` or `this` modifiers; however, `in` and `params` modifiers are permitted.
58
-
For a *record_struct_declaration*, the *record_struct_body*s `{}`, `{};`, and `;` are equivalent. They all indicate that the only members are those synthesized by the compiler (§synth-members).
59
-
60
51
### 16.2.2 Struct modifiers
61
52
62
53
A *struct_declaration* may optionally include a sequence of *struct_modifier*s:
@@ -145,7 +136,7 @@ struct_body
145
136
146
137
### 16.3.1 General
147
138
148
-
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`. For a record struct, the member set also includes the synthesized members generated by the compiler (§synth-members).
139
+
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`.
149
140
150
141
```ANTLR
151
142
struct_member_declaration
@@ -169,10 +160,6 @@ struct_member_declaration
169
160
170
161
Except for the differences noted in [§16.4](structs.md#164-class-and-struct-differences), the descriptions of class members provided in [§15.3](classes.md#153-class-members) through [§15.12](classes.md#1512-static-constructors) apply to struct members as well.
171
162
172
-
It is an error for a member of a record struct to be named `Clone`.
173
-
174
-
It is an error for an instance field of a record struct to have an unsafe type.
175
-
176
163
### 16.3.2 Readonly members
177
164
178
165
An instance member definition or accessor of an instance property, indexer, or event that includes the `readonly` modifier has the following restrictions:
@@ -213,15 +200,58 @@ An instance member definition or accessor of an instance property, indexer, or e
213
200
>
214
201
>The `readonly` method `AddMessage` canchangethestateofamessagelist. The `InitializeMessages` membercanclearandre-initializethelistofmessages. Inthecaseof `AddMessage`, the `readonly` modifierisvalid. Inthecaseof `InitializeMessages`, addingthe `readonly` modifierisinvalid. *endexample*
215
202
216
-
## §synth-members Synthesized record struct members
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](standard/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*.
218
+
219
+
A *record_struct_declaration* having a *delimited_parameter_list* declares a ***positional record struct***.
220
+
221
+
At most only one *record_struct_declaration* containing `partial` may provide a *delimited_parameter_list*.
222
+
223
+
The parameters in *delimited_parameter_list* shall not have `ref`, `out` or `this` modifiers; however, `in` and `params` modifiers are permitted.
217
224
218
-
### §synth-members-general General
225
+
### §record-structs-members Struct members
226
+
227
+
For a record struct, as well as those members identified by [§16.3](structs.md#16.3-struct-members), the member set includes the synthesized members generated by the compiler (§synth-members).
228
+
229
+
It is an error for a member of a record struct to be named `Clone`.
230
+
231
+
It is an error for an instance field of a record struct to have an unsafe type.
232
+
233
+
### §record-structs-body Record struct body
234
+
235
+
The *record_struct_body* of a record struct defines the members of that struct.
236
+
237
+
```ANTLR
238
+
record_struct_body
239
+
: struct_body ';'?
240
+
| ';'
241
+
;
242
+
```
243
+
244
+
For a *record_struct_declaration*, the *record_struct_body*s `{}`, `{};`, and `;` are equivalent. They all indicate that the only members are those synthesized by the compiler (§synth-members).
245
+
246
+
### §synth-members Synthesized record struct members
247
+
248
+
#### §synth-members-general General
219
249
220
250
In the case of a record struct, members are synthesized 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. 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.6](basic-concepts.md#76-signatures-and-overloading).)
221
251
222
252
The synthesized members are described in the following subclauses.
223
253
224
-
### §rec-struct-equalmem Equality members
254
+
####§rec-struct-equalmem Equality members
225
255
226
256
The synthesized equality members are similar to those for a record class ([§15.16.2](classes.md#15162-equality-members)), except for the lack of `EqualityContract`, null checks, or inheritance.
227
257
@@ -301,7 +331,7 @@ The synthesized override of `GetHashCode()` shall return an `int` result of comb
301
331
>
302
332
>*endexample*
303
333
304
-
### §rec-struct-prtmem Printing members
334
+
#### §rec-struct-prtmem Printing members
305
335
306
336
Arecordstructincludes a synthesized method equivalent to the following:
307
337
@@ -388,15 +418,15 @@ This method performs the following tasks:
388
418
>
389
419
>*endexample*
390
420
391
-
### §rec-struct-pos-mem Positional record struct members
421
+
#### §rec-struct-pos-mem Positional record struct members
392
422
393
-
#### §rec-struct-pos-mem-gen General
423
+
##### §rec-struct-pos-mem-gen General
394
424
395
425
Aswellasprovidingthemembersdescribedintheprecedingsubclauses, positionalrecordstructs ([§16.2.1](structs.md#1621-general)) synthesize additional members with the same conditions as the other members, as described in the following subclauses.
A record struct has a public constructor whose signature corresponds to the value parameters of the type declaration. This is called the primary constructor for the type. It is an error to have a primary constructor and a constructor with the same signature already present in the struct. If the type declaration does not include a *delimited_parameter_list*, no primary constructor is generated.
429
+
A record struct with a *delimited_parameter_list* has a public constructor whose signature corresponds to the value parameters of the type declaration. This is called the primary constructor for the type. It is an error to have a primary constructor and a constructor with the same signature already present in the struct. If the type declaration does not include a *delimited_parameter_list*, no primary constructor is generated.
0 commit comments