C#


ROSLYN.PERFORMANCE.CA1862 : Use the 'StringComparison' method overloads to perform case-insensitive string comparisons (C#)

See Roslyn-Detected C# Warning Classes for more information.

Summary

Avoid calling 'ToLower', 'ToUpper', 'ToLowerInvariant' and 'ToUpperInvariant' to perform case-insensitive string comparisons because they lead to an allocation. Instead, prefer calling the method overloads of 'Contains', 'IndexOf' and 'StartsWith' that take a 'StringComparison' enum value to perform case-insensitive comparisons. Switching to using an overload that takes a 'StringComparison' might cause subtle changes in behavior, so it's important to conduct thorough testing after applying the suggestion. Additionally, if a culturally sensitive comparison is not required, consider using 'StringComparison.OrdinalIgnoreCase'.

This check corresponds to Roslyn rule CA1862. For full rule details, see the Microsoft website: CA1862.

Properties

Class Name Use the 'StringComparison' method overloads to perform case-insensitive string comparisons (C#)
Significance reliability
Mnemonic ROSLYN.PERFORMANCE.CA1862
Categories None
Availability Available for C# only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Use the 'StringComparison' method overloads to perform case-insensitive string comparisons (C#)"

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.