C#

C#

Made by DeepSource

Replace string.Compare() with string.Equals() CS-R1079

Anti-pattern
Major
Autofix

string.Compare() returns a value that determines the lexical relationship between the two strings. It returns a -ve value if str1 precedes str2 in the sorting order, 0 if both the strings are equal, and a +ve value if str1 occurs after str2 in the sorting order. Comparing the return value against 0 is the same as checking if both the strings are equal or not. In such cases, consider rewriting it as string.Equals().

Bad Practice

var eq = string.Compare(s1, s2) == 0;

Recommended

var eq = string.Equals(s1, s2);

Reference