This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

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

Integraciones sin problemas. Prueba Datadog Code Security

PREVIEWING: guillaume.barrier/ERRORT-5095-general-doc-update