|
| 1 | +--- |
| 2 | +title: "CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons" |
| 3 | +description: "Learn about code analysis rule CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons" |
| 4 | +ms.date: 11/21/2023 |
| 5 | +f1_keywords: |
| 6 | + - "CA1862" |
| 7 | +helpviewer_keywords: |
| 8 | + - "CA1862" |
| 9 | +dev_langs: |
| 10 | + - CSharp |
| 11 | + - VB |
| 12 | +--- |
| 13 | + |
| 14 | +# CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons |
| 15 | + |
| 16 | +| Property | Value | |
| 17 | +|-------------------------------------|----------------------------------------| |
| 18 | +| **Rule ID** | CA1862 | |
| 19 | +| **Title** | Use the 'StringComparison' method overloads to perform case-insensitive string comparisons | |
| 20 | +| **Category** | [Performance](performance-warnings.md) | |
| 21 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 22 | +| **Enabled by default in .NET 8** | No | |
| 23 | + |
| 24 | +## Cause |
| 25 | + |
| 26 | +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. |
| 27 | + |
| 28 | +## Rule description |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +## How to fix violations |
| 33 | + |
| 34 | +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: |
| 35 | + |
| 36 | +- <xref:System.String.Compare(System.String,System.String,System.StringComparison)?displayProperty=nameWithType> |
| 37 | +- <xref:System.String.Contains(System.String,System.StringComparison)?displayProperty=nameWithType> |
| 38 | +- <xref:System.String.EndsWith(System.String,System.StringComparison)?displayProperty=nameWithType> |
| 39 | +- <xref:System.String.Equals(System.String,System.StringComparison)?displayProperty=nameWithType> |
| 40 | +- <xref:System.String.IndexOf%2A?displayProperty=nameWithType> |
| 41 | +- <xref:System.String.LastIndexOf%2A?displayProperty=nameWithType> |
| 42 | +- <xref:System.String.Replace(System.String,System.String,System.StringComparison)?displayProperty=nameWithType> |
| 43 | +- <xref:System.String.StartsWith(System.String,System.StringComparison)?displayProperty=nameWithType> |
| 44 | +- <xref:System.Uri.Compare(System.Uri,System.Uri,System.UriComponents,System.UriFormat,System.StringComparison)> |
| 45 | +- <xref:Microsoft.Extensions.Primitives.StringSegment.Compare(Microsoft.Extensions.Primitives.StringSegment,Microsoft.Extensions.Primitives.StringSegment,System.StringComparison)?displayProperty=nameWithType> |
| 46 | +- <xref:Microsoft.Extensions.Primitives.StringSegment.EndsWith(System.String,System.StringComparison)?displayProperty=nameWithType> |
| 47 | +- <xref:Microsoft.Extensions.Primitives.StringSegment.Equals%2A?displayProperty=nameWithType> |
| 48 | +- <xref:Microsoft.Extensions.Primitives.StringSegment.StartsWith(System.String,System.StringComparison)?displayProperty=nameWithType> |
| 49 | + |
| 50 | +> [!NOTE] |
| 51 | +> |
| 52 | +> - 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. |
| 53 | +> - If the strings don't need to be compared in a culturally sensitive manner, consider passing <xref:System.StringComparison.OrdinalIgnoreCase?displayProperty=nameWithType>. |
| 54 | +
|
| 55 | +## Example |
| 56 | + |
| 57 | +The following example shows a violation of the rule: |
| 58 | + |
| 59 | +```csharp |
| 60 | +string s1 = "aBc"; |
| 61 | +string s2 = "aBC"; |
| 62 | + |
| 63 | +int _ = s1.ToUpper().CompareTo(s2.ToUpper()); |
| 64 | +``` |
| 65 | + |
| 66 | +```vb |
| 67 | +Dim s1 As String = "aBc" |
| 68 | +Dim s2 As String = "aBC" |
| 69 | + |
| 70 | +Dim i As Integer = s1.ToUpper().CompareTo(s2.ToUpper()) |
| 71 | +``` |
| 72 | + |
| 73 | +The following example shows code that fixes the violation: |
| 74 | + |
| 75 | +```csharp |
| 76 | +string s1 = "aBc"; |
| 77 | +string s2 = "aBC"; |
| 78 | + |
| 79 | +int _ = StringComparer.CurrentCultureIgnoreCase.Compare(s1, s2); |
| 80 | +``` |
| 81 | + |
| 82 | +```vb |
| 83 | +Dim s1 As String = "aBc" |
| 84 | +Dim s2 As String = "aBC" |
| 85 | + |
| 86 | +Dim i As Integer = StringComparer.CurrentCultureIgnoreCase.Compare(s1, s2) |
| 87 | +``` |
| 88 | + |
| 89 | +## When to suppress warnings |
| 90 | + |
| 91 | +It's safe to suppress warnings from this rule if performance isn't a concern. |
| 92 | + |
| 93 | +## Suppress a warning |
| 94 | + |
| 95 | +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 96 | + |
| 97 | +```csharp |
| 98 | +#pragma warning disable CA1862 |
| 99 | +// The code that's violating the rule is on this line. |
| 100 | +#pragma warning restore CA1862 |
| 101 | +``` |
| 102 | + |
| 103 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 104 | + |
| 105 | +```ini |
| 106 | +[*.{cs,vb}] |
| 107 | +dotnet_diagnostic.CA1862.severity = none |
| 108 | +``` |
| 109 | + |
| 110 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
0 commit comments