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
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.
25
25
26
26
A struct declaration shall not supply a *type_parameter_constraints_clauses* unless it also supplies a *type_parameter_list*.
27
27
@@ -38,6 +38,7 @@ struct_modifier
38
38
| 'protected'
39
39
| 'internal'
40
40
| 'private'
41
+
| 'readonly'
41
42
| unsafe_modifier // unsafe code support
42
43
;
43
44
```
@@ -46,7 +47,32 @@ struct_modifier
46
47
47
48
It is a compile-time error for the same modifier to appear multiple times in a struct declaration.
48
49
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.
0 commit comments