Skip to content

Commit 6d2b5df

Browse files
RexJaeschkeBillWagner
authored andcommitted
Update structs.md
1 parent 82f553c commit 6d2b5df

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

standard/structs.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ A *struct_declaration* is a *type_declaration* ([§13.7](namespaces.md#137-type-
1616

1717
```ANTLR
1818
struct_declaration
19-
: attributes? struct_modifier* 'partial'? 'struct' identifier type_parameter_list?
19+
: attributes? struct_modifier* 'ref'? 'partial'? 'struct' identifier type_parameter_list?
2020
struct_interfaces? type_parameter_constraints_clause* struct_body ';'?
2121
;
2222
```
2323

24-
A *struct_declaration* consists of an optional set of *attributes* ([§21](attributes.md#21-attributes)), followed by an optional set of *struct_modifier*s ([§15.2.2](structs.md#1522-struct-modifiers)), followed by an optional partial modifier ([§14.2.7](classes.md#1427-partial-declarations)), followed by the keyword `struct` and an *identifier* that names the struct, followed by an optional *type_parameter_list* specification ([§14.2.3](classes.md#1423-type-parameters)), followed by an optional *struct_interfaces* specification ([§15.2.4](structs.md#1524-struct-interfaces)), followed by an optional *type_parameter_constraints-clauses* specification ([§14.2.5](classes.md#1425-type-parameter-constraints)), followed by a *struct_body* ([§15.2.5](structs.md#1525-struct-body)), optionally followed by a semicolon.
24+
A *struct_declaration* consists of an optional set of *attributes* ([§21](attributes.md#21-attributes)), followed by an optional set of *struct_modifier*s ([§15.2.2](structs.md#1522-struct-modifiers)), followed by an optional `ref` modifier (§ref-modifier-new-clause), followed by an optional partial modifier ([§14.2.7](classes.md#1427-partial-declarations)), followed by the keyword `struct` and an *identifier* that names the struct, followed by an optional *type_parameter_list* specification ([§14.2.3](classes.md#1423-type-parameters)), followed by an optional *struct_interfaces* specification ([§15.2.4](structs.md#1524-struct-interfaces)), followed by an optional *type_parameter_constraints-clauses* specification ([§14.2.5](classes.md#1425-type-parameter-constraints)), followed by a *struct_body* ([§15.2.5](structs.md#1525-struct-body)), optionally followed by a semicolon.
2525

2626
A struct declaration shall not supply a *type_parameter_constraints_clauses* unless it also supplies a *type_parameter_list*.
2727

@@ -38,6 +38,7 @@ struct_modifier
3838
| 'protected'
3939
| 'internal'
4040
| 'private'
41+
| 'readonly'
4142
| unsafe_modifier // unsafe code support
4243
;
4344
```
@@ -46,7 +47,32 @@ struct_modifier
4647

4748
It is a compile-time error for the same modifier to appear multiple times in a struct declaration.
4849

49-
The modifiers of a struct declaration have the same meaning as those of a class declaration ([§14.2.2](classes.md#1422-class-modifiers)).
50+
Except for `readonly`, the modifiers of a struct declaration have the same meaning as those of a class declaration ([§14.2.2](classes.md#1422-class-modifiers)).
51+
52+
The `readonly` modifier indicates that the *struct_declaration* declares a type whose instances are immutable.
53+
54+
A readonly struct has the following constraints:
55+
56+
- Each of its instance fields shall also be declared `readonly`.
57+
- None of its instance properties shall have a *set_accessor_declaration* ([§14.7.3](classes.md#1473-accessors)).
58+
- It shall not declare any field-like events ([§14.8.2](classes.md#1482-field-like events)).
59+
60+
When an instance of a readonly struct is passed to a method, its `this` is treated like an `in` argument/parameter, which disallows write access to any instance fields (except by constructors).
61+
62+
### §ref-modifier-new-clause Ref modifier
63+
64+
The `ref` modifier indicates that the *struct_declaration* declares a type whose instances are allocated on the execution stack.
65+
66+
It is a compile-time error if a ref struct type is used in any of the following contexts:
67+
- As the element type of an array.
68+
- As the declared type of a field of a class or a non-ref struct.
69+
- To implement an interface.
70+
- Being boxed to `System.ValueType` or `System.Object`.
71+
- As a type argument.
72+
- An async method.
73+
- An iterator.
74+
75+
A ref struct can't be captured by a lambda expression or a local function.
5076

5177
### 15.2.3 Partial modifier
5278

0 commit comments

Comments
 (0)