Skip to content

Latest commit

 

History

History
112 lines (82 loc) · 5.78 KB

File metadata and controls

112 lines (82 loc) · 5.78 KB
title description ms.date f1_keywords helpviewer_keywords dev_langs
CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
Learn about code analysis rule CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
11/21/2023
CA1862
CA1862
CSharp
VB

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 xref:System.String.ToLower, xref:System.String.ToLowerInvariant, xref:System.String.ToUpper, or xref:System.String.ToUpperInvariant on one or both strings.

Rule description

When code calls xref:System.String.ToLower, xref:System.String.ToLowerInvariant, xref:System.String.ToUpper, or xref:System.String.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 xref:System.StringComparison and specify one of the *IgnoreCase values.

How to fix violations

Remove the call to xref:System.String.ToLower, xref:System.String.ToLowerInvariant, xref:System.String.ToUpper, or xref:System.String.ToUpperInvariant, and either call one of the xref:System.StringComparer methods, or one of the following methods that takes a xref:System.StringComparison argument:

  • xref:System.String.Compare(System.String,System.String,System.StringComparison)?displayProperty=nameWithType
  • xref:System.String.Contains(System.String,System.StringComparison)?displayProperty=nameWithType
  • xref:System.String.EndsWith(System.String,System.StringComparison)?displayProperty=nameWithType
  • xref:System.String.Equals(System.String,System.StringComparison)?displayProperty=nameWithType
  • xref:System.String.IndexOf%2A?displayProperty=nameWithType
  • xref:System.String.LastIndexOf%2A?displayProperty=nameWithType
  • xref:System.String.Replace(System.String,System.String,System.StringComparison)?displayProperty=nameWithType
  • xref:System.String.StartsWith(System.String,System.StringComparison)?displayProperty=nameWithType
  • xref:System.Uri.Compare(System.Uri,System.Uri,System.UriComponents,System.UriFormat,System.StringComparison)
  • xref:Microsoft.Extensions.Primitives.StringSegment.Compare(Microsoft.Extensions.Primitives.StringSegment,Microsoft.Extensions.Primitives.StringSegment,System.StringComparison)?displayProperty=nameWithType
  • xref:Microsoft.Extensions.Primitives.StringSegment.EndsWith(System.String,System.StringComparison)?displayProperty=nameWithType
  • xref:Microsoft.Extensions.Primitives.StringSegment.Equals%2A?displayProperty=nameWithType
  • xref:Microsoft.Extensions.Primitives.StringSegment.StartsWith(System.String,System.StringComparison)?displayProperty=nameWithType

Note

  • If you change your code to use an overload that takes a xref:System.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 xref:System.StringComparison.OrdinalIgnoreCase?displayProperty=nameWithType.

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 xref:System.String.Equals(System.String,System.StringComparison)?displayProperty=nameWithType that takes a xref:System.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.