Muokkaa

Jaa


CA1311: Specify a culture or use an invariant version

Property Value
Rule ID CA1311
Title Specify a culture or use an invariant version
Category Globalization
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 9 No

Cause

A call is made to String.ToUpper() or String.ToLower() without specifying a culture.

Rule description

Specify a culture or use an invariant culture to avoid implicit dependency on the current culture when calling ToUpper or ToLower. Using an invariant culture yields consistent results regardless of the culture of an application.

How to fix violations

Instead of calling the parameterless String.ToUpper() or String.ToLower() methods, call ToUpper(CultureInfo) or ToUpperInvariant(), or ToLower(CultureInfo) or ToLowerInvariant().

Example

The following code snippet shows a violation of rule CA1311:

string s = "hello";
s = s.ToLower();
Dim s As String = "hello"
s.ToLower()

The following code snippet fixes the violation:

string s = "hello";
s = s.ToLowerInvariant();
Dim s As String = "hello"
s.ToLowerInvariant()

When to suppress warnings

It's safe to suppress a warning from this rule if you're certain that Thread.CurrentCulture will never change.

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 CA1311
// The code that's violating the rule is on this line.
#pragma warning restore CA1311

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA1311.severity = none

For more information, see How to suppress code analysis warnings.

See also