coderushforroslyn-401176-static-code-analysis-analyzers-library-crr0050-string-compare-can-be-used.md
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.
bool CheckError(string msg) {
if (msg.Substring(1, 4) == "Error")
return true;
return false;
}
Function CheckError(ByVal msg As String) As Boolean
If msg.Substring(1, 4) = "Error" Then
Return True
End If
Return False
End Function
To fix this issue, use the string.Compare method call instead of the == (= in Visual Basic) or != operator (<> in Visual Basic):
bool CheckError(string msg) {
if (string.Compare(msg.Substring(1, 4), "Error", false) == 0)
return true;
return false;
}
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.
You may want to suppress CRR0050 if your comparison is strictly for equality.
Use string.Compare when:
Use == when:
See Also