All rules
CA1310Globalization Enabled by default: No MS default: Warning

Specify StringComparison for correctness

Pass an explicit StringComparison to string operations.

Microsoft docs

Description

String comparison methods that use the current culture by default should be called with an explicit StringComparison.

Cause

A string comparison operation uses a method overload that does not set a System.StringComparison parameter and uses culture-specific string comparison by default. Hence, its behavior will vary based on the current user's locale settings.

Why it matters

Implicit culture-sensitive comparisons cause subtle, hard-to-reproduce bugs across locales.

How to fix violations

To fix a violation of this rule, change string comparison methods to overloads that accept the System.StringComparison enumeration as a parameter. For example, change String.Compare(str1, str2) to String.Compare(str1, str2, StringComparison.Ordinal).

Examples

Avoid
if (name.StartsWith("admin")) { }
Prefer
if (name.StartsWith("admin", StringComparison.Ordinal)) { }

When to suppress

It is safe to suppress a warning from this rule when the library or application is not intended to be localized.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0