A call is made to String.ToUpper() or String.ToLower() without specifying a culture.
Rule descriptionSpecify 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.
Instead of calling the parameterless String.ToUpper() or String.ToLower() methods, call ToUpper(CultureInfo) or ToUpperInvariant(), or ToLower(CultureInfo) or ToLowerInvariant().
ExampleThe 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 warningIf 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 alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4