CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons

Property Value
Rule ID CA1862
Title Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
Category Performance
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 8 As suggestion

Cause

Code compares two strings in a case-insensitive manner by first calling ToLower(), ToLowerInvariant(), ToUpper(), or ToUpperInvariant() on one or both strings.

Rule description

When code calls ToLower(), ToLowerInvariant(), ToUpper(), or ToUpperInvariant(), an allocation is performed. If the only reason for calling these methods is to perform a case-insensitive string comparison or search, the allocation is unnecessary. Instead, you can call a string comparison method that takes a StringComparison and specify one of the *IgnoreCase values.

How to fix violations

Remove the call to ToLower(), ToLowerInvariant(), ToUpper(), or ToUpperInvariant(), and either call one of the StringComparer methods, or one of the following methods that takes a StringComparison argument:

Note

  • If you change your code to use an overload that takes a StringComparison argument, it might cause subtle changes in behavior. It's important to conduct thorough testing if you make this change or accept the Visual Studio lightbulb suggestion.
  • If the strings don't need to be compared in a culturally sensitive manner, consider passing StringComparison.OrdinalIgnoreCase.

Example

The following example shows a violation of the rule:

string s1 = "aBc";
string s2 = "aBC";

int _ = s1.ToUpper().CompareTo(s2.ToUpper());
Dim s1 As String = "aBc"
Dim s2 As String = "aBC"

Dim i As Integer = s1.ToUpper().CompareTo(s2.ToUpper())

The following example shows code that fixes the violation:

string s1 = "aBc";
string s2 = "aBC";

int _ = StringComparer.CurrentCultureIgnoreCase.Compare(s1, s2);
Dim s1 As String = "aBc"
Dim s2 As String = "aBC"

Dim i As Integer = StringComparer.CurrentCultureIgnoreCase.Compare(s1, s2)

When to suppress warnings

It's safe to suppress warnings from this rule if performance isn't a concern.

If you're using Entity Framework Core (EF Core), you should suppress this rule for scenarios where you're querying a database by comparing a string. EF Core throws an exception if you use a method such as String.Equals(String, StringComparison) that takes a StringComparison argument, as it won't translate such queries to SQL. For more information, see Translation of built-in .NET string operations.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA1862
// The code that's violating the rule is on this line.
#pragma warning restore CA1862

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1862.severity = none

For more information, see How to suppress code analysis warnings.