This product is not supported for your selected Datadog site. ().
このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください

Metadata

ID: csharp-best-practices/ensure-self-type-parameter

Language: C#

Severity: Error

Category: Error Prone

Description

This rule enforces the correct usage of the TSelf parameter in C#. When implementing an interface that requires a TSelf type parameter, such as IParsable<TSelf>, the TSelf parameter should be the same as the type that is implementing the interface. This is important because it ensures type safety and allows for correct usage of the interface’s methods.

In the non-compliant code example, the class MyDate is implementing the IParsable<TSelf> interface but is using DateOnly for the TSelf parameter. This is incorrect because the TSelf parameter should be MyDate, the type that is implementing the interface.

To avoid violating this rule, always use the type that is implementing the interface for the TSelf parameter.

Non-Compliant Code Examples

using System;

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

Compliant Code Examples

using System;

public readonly class MyDate : IParsable<MyDate> { 
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

シームレスな統合。 Datadog Code Security をお試しください