diff --git a/docs/csharp/language-reference/builtin-types/struct.md b/docs/csharp/language-reference/builtin-types/struct.md index ee13a5c445f89..c0a5a78ccca77 100644 --- a/docs/csharp/language-reference/builtin-types/struct.md +++ b/docs/csharp/language-reference/builtin-types/struct.md @@ -24,11 +24,7 @@ Because structure types have value semantics, we recommend you to define *immuta ## `readonly` struct -Beginning with C# 7.2, you use the `readonly` modifier to declare that a structure type is immutable: - -[!code-csharp[readonly struct](snippets/shared/StructType.cs#ReadonlyStruct)] - -All data members of a `readonly` struct must be read-only as follows: +Beginning with C# 7.2, you use the `readonly` modifier to declare that a structure type is immutable. All data members of a `readonly` struct must be read-only as follows: - Any field declaration must have the [`readonly` modifier](../keywords/readonly.md) - Any property, including auto-implemented ones, must be read-only. In C# 9.0 and later, a property may have an [`init` accessor](../../whats-new/csharp-9.md#init-only-setters). @@ -38,6 +34,10 @@ That guarantees that no member of a `readonly` struct modifies the state of the > [!NOTE] > In a `readonly` struct, a data member of a mutable reference type still can mutate its own state. For example, you can't replace a instance, but you can add new elements to it. +The following code defines a `readonly` struct with init-only property setters, available in C# 9.0 and later: + +[!code-csharp[readonly struct](snippets/shared/StructType.cs#ReadonlyStruct)] + ## `readonly` instance members Beginning with C# 8.0, you can also use the `readonly` modifier to declare that an instance member doesn't modify the state of a struct. If you can't declare the whole structure type as `readonly`, use the `readonly` modifier to mark the instance members that don't modify the state of the struct.