Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 9 | As suggestion |
Code compares two strings in a case-insensitive manner by first calling ToLower(), ToLowerInvariant(), ToUpper(), or ToUpperInvariant() on one or both strings.
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.
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
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)
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.
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.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register now