Skip to main content

String.Compare can be used

In This Article

CodeRush Classic shows the String.Compare can be used code issue if the == operator is used to compare two string values.

#Fix

Use a String.Compare() call instead of the == operator.

#Purpose

Highlights the string comparison expressions, which should be replaced with a String.Compare() call to improve code readability and provide additional comparison options.

#Example

private string _Text;
public bool ChangeText(string text)
{
    if (_Text == text)
        return false;
    _Text = text;
    return true;
}

Fix:

private string _Text;
public bool ChangeText(string text)
{
    if (String.Compare(_Text, text, false) == 0)
        return false;
    _Text = text;
    return true;
}