All rules
CA2260Usage Enabled by default: As warning

Implement generic math interfaces correctly

Implement generic math interfaces correctly

Microsoft docs

Description

Some generic math interfaces introduce static abstract members. The only way to access those static members is through a generic constraint that implements the "curiously recurring template pattern" (CRTP). Therefore, the derived type itself must be used for the self-recurring type parameter. If a type implements such an interface without passing the required type parameter and CA2260 is ignored, the code will compile successfully but the static abstract will not be accessible. Thus, the type will not be usable. The compiler emits a warning with ID CS0315 on such usage.

Cause

This rule fires when you implement a generic math interface that requires a self-recurring type parameter and you don't pass the type itself as the type parameter.

How to fix violations

Pass correct type parameter for self recurring type parameter (TSelf) when implementing those interfaces.

### Example

Violation:

Fix:

Pass the MyDate type as the type parameter for the IParsable<TSelf> interface.

Example

using System;

// Warns: The 'IParsable<TSelf>' requires the 'TSelf' type parameter to be filled with the derived type 'MyDate'
public readonly struct MyDate : IParsable<DateOnly>
{ ... }

When to suppress

Do not suppress a warning from this rule.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0