diff --git a/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md b/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md index 820523fd27ff8..9df47c00225eb 100644 --- a/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md +++ b/docs/csharp/language-reference/compiler-messages/ref-struct-errors.md @@ -37,7 +37,7 @@ ms.date: 07/30/2024 - [**CS8344**](#ref-struct-interface-implementations): *`foreach` statement cannot operate on enumerators in async or iterator methods because type is a `ref struct` or a type parameter that allows `ref struct`.* - [**CS8345**](#ref-safety-violations): *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.* - [**CS9048**](#ref-safety-violations): *The `scoped` modifier can be used for refs and `ref struct` values only.* -- [**CS9050**](#compiler-error-cs9050): *A `ref` field cannot refer to a `ref struct`.* +- [**CS9050**](#ref-safety-violations): *A `ref` field cannot refer to a `ref struct`.* - [**CS9059**](#ref-safety-violations): *A ref field can only be declared in a ref struct.* - [**CS9241**](#ref-struct-interface-implementations): *'ref struct' is already specified.* - [**CS9242**](#ref-struct-interface-implementations): *The 'allows' constraint clause must be the last constraint specified.* @@ -45,45 +45,42 @@ ms.date: 07/30/2024 - [**CS9244**](#ref-struct-interface-implementations): *The type may not be a `ref struct` or a type parameter allowing ref structs in order to use it as parameter in the generic type or method.* - [**CS9245**](#ref-struct-interface-implementations): *Type cannot implement interface member for `ref struct` type.* - [**CS9246**](#ref-struct-interface-implementations): *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.* -- [**CS9247**](#ref-struct-interface-implementations): *foreach statement cannot operate on enumerators of type '{0}' because it is a type parameter that allows ref struct and it is not known at compile time to implement IDisposable.* +- [**CS9247**](#ref-struct-interface-implementations): *foreach statement cannot operate on enumerators of type because it is a type parameter that allows ref struct and it is not known at compile time to implement `IDisposable`.* -## ref struct interface implementations +## ref safety violations -More content here. +- **CS8345**: *Field or auto-implemented property cannot be of type unless it is an instance member of a `ref struct`.* +- **CS9048**: *The `scoped` modifier can be used for refs and `ref struct` values only.* +- **CS9050**: *A `ref` field cannot refer to a `ref struct`.* +- **CS9059**: *A `ref` field can only be declared in a `ref struct`.* -## ref safety violations +A [`ref struct`](../builtin-types/ref-struct.md) type can include `ref` fields. Other types aren't allowed `ref` fields. The compiler enforces restrictions on the declarations and use of `ref struct` types to enforce ref safety rules on instances of any `ref struct` type: -Even more content goes here. +- Only `ref struct` types can contain auto-implemented `ref` properties. +- Only `ref struct` types or `ref` variables can have the `scoped` modifier. +- A `ref` field can be declared only in a `ref struct` type. +- A `ref` field can't refer to a `ref struct` type/ -## Compiler Error CS9050 +Violating any of these rules produces one of the listed errors. If you intended to use that language feature, convert the type to a `ref struct`. Otherwise, remove the disallowed construct. -A `ref` field cannot refer to a `ref struct`. +## ref struct interface implementations -The compiler does not support the `ref` modifier on a field within a struct (to declare a stack-allocated field) of a type already declared stack-allocated (`ref struct`). +- **CS8343**: *`ref structs` cannot implement interfaces* +- **CS8344**: *`foreach` statement cannot operate on enumerators in async or iterator methods because type is a `ref struct` or a type parameter that allows `ref struct`.* +- **CS9241**: *'ref struct' is already specified.* +- **CS9242**: *The 'allows' constraint clause must be the last constraint specified.* +- **CS9243**: *Cannot allow ref structs for a type parameter known from other constraints to be a class.* +- **CS9244**: *The type may not be a `ref struct` or a type parameter allowing ref structs in order to use it as parameter in the generic type or method.* +- **CS9245**: *Type cannot implement interface member for `ref struct` type.* +- **CS9246**: *A non-virtual instance interface member cannot be accessed on a type parameter that allows ref struct.* +- **CS9247**: *foreach statement cannot operate on enumerators of type because it is a type parameter that allows ref struct and it is not known at compile time to implement `IDisposable`.* -The following sample generates CS9050: +Prior to C# 13, [`ref struct`](../builtin-types/ref-struct.md) types can't implement interfaces; the compiler generates *CS8343*. Beginning with C# 13, `ref struct` types can implement interfaces, subject to the following rules: -```csharp -// CS9050.cs (0,0) -ref struct Color -{ - public float r, g, b; -} -ref struct Group -{ - public ref Color color; -} -``` +- A `ref struct` can't be converted to an instance of an interface it implements. This restriction includes the implicit conversion when you use a `ref struct` type as an argument when the parameter is an interface type. The conversion results in a boxing conversion, which violates ref safety. +- A `ref struct` that implements an interface *must* implement all interface members. The `ref struct` must implement members where the interface includes a default implementation. -In this example, it is most likely a typo to have included a `ref` modifier on a field of a `ref struct` type within the declaration of a `ref struct`. Removing the `ref` modifier corrects this error. +Beginning with C# 13, a `ref struct` can be used as a type argument for a generic type parameter, if and only if the generic type parameter has the [`allows ref struct`](../../programming-guide/generics/constraints-on-type-parameters.md#allows-ref-struct) anti-constraint. When you use the `allows ref struct` anti-constraint you must follow these rules: -```csharp -ref struct Color -{ - public float r, g, b; -} -ref struct Group -{ - public Color color; -} -``` +- A `ref struct` is used as a type argument, the type parameter *must* have the `allows ref struct` anti-constraint.- The `allows ref struct` anti-constraint must be last in the `where` clause for that parameter +- Uses of instances the type parameter must obey ref safety rules.