This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: csharp-best-practices/condition-always-true

Language: C#

Severity: Info

Category: Best Practices

Description

In C#, having conditions that are always true (or always false) is generally a bad practice because it leads to unreachable code, logic errors, and maintainability issues.

How to Remediate

  • Replace true with a meaningful, dynamic condition.
  • Use variables, method results, or flags to evaluate logic properly.
  • Remove useless condition branches

Non-Compliant Code Examples

// Count cannot be negative
if (myList.Count < 0) {
    // do something
}

// use double.IsNan instead
if (x == double.NaN) {
    // do something
}

Compliant Code Examples


if (myList.Count >= 0) {
    // do something
}


if (double.isNaN(x)) {
    // do something
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security

PREVIEWING: ida.adjivon/rtrieu/DOCS-10715-ET-standalone