Note
Ang pag-access sa pahinang ito ay nangangailangan ng pahintulot. Maaari mong subukang mag-sign in o magpalit ng mga direktoryo.
Ang pag-access sa pahinang ito ay nangangailangan ng pahintulot. Maaari mong subukang baguhin ang mga direktoryo.
The compiler generates the following errors for invalid declarations of static abstract or virtual members in interfaces:
- CS8920: The interface cannot be used as type argument. Static member does not have a most specific implementation in the interface.
- CS8921: The parameter of a unary operator must be the containing type, or its type parameter constrained to it.
- CS8922: The parameter type for
++or--operator must be the containing type, or its type parameter constrained to it. - CS8923: The return type for
++or--operator must either match the parameter type, or be derived from the parameter type, or be the containing type's type parameter constrained to it unless the parameter type is a different type parameter. - CS8924: One of the parameters of a binary operator must be the containing type, or its type parameter constrained to it.
- CS8925: The first operand of an overloaded shift operator must have the same type as the containing type or its type parameter constrained to it
- CS8926: A static virtual or abstract interface member can be accessed only on a type parameter.
- CS8928: Type does not implement static interface member. The method cannot implement the interface member because it is not static.
- CS8930: Explicit implementation of a user-defined operator must be declared static
- CS8931: User-defined conversion in an interface must convert to or from a type parameter on the enclosing type constrained to the enclosing type
- CS8932: 'UnmanagedCallersOnly' method cannot implement interface member in type
- CS9044: Type does not implement interface member. Method cannot implicitly implement an inaccessible member.
- CS9046: One of the parameters of an equality or inequality operator declared in an interface must be a type parameter constrained to the interface
These errors occur in three places in your code:
- When you declare an interface with static abstract or virtual members,
- When you declare a type that implements an interface with static abstract or virtual members, and
- When you access a static abstract or virtual method declared in an interface.
Errors in interface declaration
You might encounter the following errors when you declare an interface with static abstract or static virtual members:
- CS8921: The parameter of a unary operator must be the containing type, or its type parameter constrained to it.
- CS8922: The parameter type for
++or--operator must be the containing type, or its type parameter constrained to it. - CS8923: The return type for
++or--operator must either match the parameter type, or be derived from the parameter type, or be the containing type's type parameter constrained to it unless the parameter type is a different type parameter. - CS8924: One of the parameters of a binary operator must be the containing type, or its type parameter constrained to it.
- CS8925: The first operand of an overloaded shift operator must have the same type as the containing type or its type parameter constrained to it
- CS8931: User-defined conversion in an interface must convert to or from a type parameter on the enclosing type constrained to the enclosing type
- CS9046: One of the parameters of an equality or inequality operator declared in interface must be a type parameter constrained to the interface
For unary operators declared in an interface, ensure the parameter is either the interface type itself or a type parameter T where T is constrained to implement the interface (CS8921). This constraint ensures the operator can only be applied to types that implement the interface, enabling the compiler to resolve the correct implementation at compile time.
For increment (++) and decrement (--) operators, verify that the parameter follows the same rules as other unary operators (CS8922). Additionally, the return type must either match the parameter type, derive from it, or be the interface's type parameter constrained to the interface (CS8923). These rules ensure that increment and decrement operations return a compatible type that can be assigned back to the original variable.
For binary operators, at least one of the two parameters must be the containing interface type or a type parameter constrained to implement the interface (CS8924). This requirement allows the other parameter to be any type, enabling operators like T operator +(T left, int right) in generic math scenarios.
For shift operators (<< and >>), the first operand must be the containing type or its constrained type parameter (CS8925). The second operand follows standard shift operator rules and is typically int.
For user-defined conversion operators, the conversion must involve a type parameter that is constrained to the enclosing interface type (CS8931). You can't define conversions between arbitrary types in an interface; the conversion must relate to types that implement the interface.
For equality (==) and inequality (!=) operators, at least one parameter must be a type parameter constrained to the interface, not just the interface type itself (CS9046). This stricter requirement for equality operators ensures proper type safety when comparing instances through the interface.
For more information about the rules for operator declarations in interfaces, see static abstract members in interfaces. For a practical guide to implementing these patterns, see Explore static abstract interface members.
Errors in type implementing interface declaration
You might encounter the following errors when you define a type that implements an interface with static abstract or static virtual methods:
- CS8928: Type does not implement static interface member. The method cannot implement the interface member because it is not static.
- CS8930: Explicit implementation of a user-defined operator must be declared static
- CS8932: 'UnmanagedCallersOnly' method cannot implement interface member in type
- CS9044: Type does not implement interface member. Method cannot implicitly implement an inaccessible member.
When you implement a static abstract or static virtual interface member, declare the implementing method by using the static modifier (CS8928). Unlike instance interface members that are implemented by instance methods, static abstract members require static implementations because the runtime invokes them on the type itself, not on an instance.
For explicit implementations of user-defined operators from an interface, include the static modifier in the implementation (CS8930). Explicit interface implementations of operators follow the same static requirement as implicit implementations.
Remove the System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute attribute from any method that implements an interface member (CS8932). Methods marked by using this attribute can only be called from unmanaged code and can't participate in interface implementation because the runtime needs to call them through the interface dispatch mechanism.
If the implementing method has more restrictive accessibility than the interface member (for example, a private or internal method implementing a public interface member), use explicit interface implementation syntax instead of implicit implementation (CS9044). Implicit implementation requires the implementing member to be at least as accessible as the interface member it implements.
For more information about implementing interface members, see Interfaces and explicit interface implementation.
Errors calling static abstract interface members
You might see the following errors when you try to call a member defined as a static abstract or static virtual member of an interface:
- CS8920: The interface cannot be used as type argument. Static member does not have a most specific implementation in the interface.
- CS8926: A static virtual or abstract interface member can be accessed only on a type parameter.
When you use an interface with static abstract members as a type argument, make sure that all static abstract members have a most specific implementation available (CS8920). You see this error when the compiler can't determine which implementation to use, typically because multiple interface hierarchies provide conflicting default implementations or no implementation exists.
Access static abstract or static virtual interface members through a type parameter that is constrained to implement the interface, rather than through the interface type directly (CS8926). For example, use T.MemberName where T is constrained by where T : IMyInterface, rather than IMyInterface.MemberName. The compiler needs a concrete type to resolve which implementation to call, and a constrained type parameter provides that concrete type at compile time through generic specialization.
For more information about accessing static abstract members, see static abstract members in interfaces.