Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Errors and warnings associated with
- CS8343:
ref structscannot implement interfaces - CS8344:
foreachstatement cannot operate on enumerators in async or iterator methods because type is aref structor a type parameter that allowsref struct. - CS8345: Field or auto-implemented property cannot be of type unless it is an instance member of a
ref struct. - CS9048: The
scopedmodifier can be used for refs andref structvalues only. - CS9050: A
reffield cannot refer to aref struct. - CS9059: A ref field can only be declared in a 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 structor 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 structtype. - 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. - CS9267: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
ref safety violations
- CS8345: Field or auto-implemented property cannot be of type unless it is an instance member of a
ref struct. - CS9048: The
scopedmodifier can be used for refs andref structvalues only. - CS9050: A
reffield cannot refer to aref struct. - CS9059: A
reffield can only be declared in aref struct.
A ref struct 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:
- Only
ref structtypes can contain automatically implementedrefproperties. - Only
ref structtypes orrefvariables can have thescopedmodifier. - A
reffield can be declared only in aref structtype. - A
reffield can't refer to aref structtype/
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.
ref struct interface implementations
- CS8343:
ref structscannot implement interfaces - CS8344:
foreachstatement cannot operate on enumerators in async or iterator methods because type is aref structor a type parameter that allowsref 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 structor 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 structtype. - 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. - CS9267: Element type of an iterator may not be a ref struct or a type parameter allowing ref structs
Prior to C# 13, ref struct types can't implement interfaces; the compiler generates CS8343. Beginning with C# 13, ref struct types can implement interfaces, subject to the following rules:
- A
ref structcan't be converted to an instance of an interface it implements. This restriction includes the implicit conversion when you use aref structtype as an argument when the parameter is an interface type. The conversion results in a boxing conversion, which violates ref safety. - A
ref structthat implements an interface must implement all interface members. Theref structmust implement members where the interface includes a default implementation.
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 anti-constraint. When you use the allows ref struct anti-constraint you must follow these rules:
- A
ref structis used as a type argument, the type parameter must have theallows ref structanti-constraint.- Theallows ref structanti-constraint must be last in thewhereclause for that parameter - Uses of instances the type parameter must obey ref safety rules.
- A
ref structor a type argument that can be aref structtype can't be used as the element type for an iterator method.