Back to Devexpress

CRR0050 - String.Compare can be used

coderushforroslyn-401176-static-code-analysis-analyzers-library-crr0050-string-compare-can-be-used.md

latest2.1 KB
Original Source

CRR0050 - String.Compare can be used

  • Aug 06, 2025
  • 2 minutes to read

This analyzer identifies string comparison expressions which can be replaced with a string.Compare call to improve code readability and provide comparison options such as culture-specific or case-insensitive comparisons.

csharp
bool CheckError(string msg) {
    if (msg.Substring(1, 4) == "Error")
        return true;
    return false;
}
vb
Function CheckError(ByVal msg As String) As Boolean
        If msg.Substring(1, 4) = "Error" Then
            Return True
        End If
        Return False
End Function

How to Fix Violations

To fix this issue, use the string.Compare method call instead of the == (= in Visual Basic) or != operator (<> in Visual Basic):

csharp
bool CheckError(string msg) {
    if (string.Compare(msg.Substring(1, 4), "Error", false) == 0)
        return true;
    return false;
}
vb
Function CheckError(ByVal msg As String) As Boolean
    If String.Compare(msg.Substring(1, 4), "Error", False) = 0 Then
        Return True
    End If
    Return False
End Function

Call the Use string.Compare refactoring to convert the comparison with the equality operator or inequality operator to a comparison with the string.Compare method.

When to Suppress Warnings

You may want to suppress CRR0050 if your comparison is strictly for equality.

Use string.Compare when:

  • You need to determine ordering between strings (for example, sorting or lexicographic comparison)
  • You require culture-aware comparisons (for example, the Turkish i problem)
  • You want case-insensitive comparisons (for example, a file path in Windows)

Use == when:

  • You want a simple, case-sensitive, ordinal equality check
  • You compare strings in performance-critical code and do not need culture or case flexibility

See Also

Suppress Analyzers