title | description | ms.date | f1_keywords | helpviewer_keywords | author | ms.author | ||||
---|---|---|---|---|---|---|---|---|---|---|
CA1304: Specify CultureInfo (code analysis) |
Learn about code analysis rule CA1304: Specify CultureInfo |
06/30/2018 |
|
|
gewarren |
gewarren |
Property | Value |
---|---|
Rule ID | CA1304 |
Title | Specify CultureInfo |
Category | Globalization |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 8 | No |
A method or constructor calls a member that has an overload that accepts a xref:System.Globalization.CultureInfo?displayProperty=nameWithType parameter, and the method or constructor does not call the overload that takes the xref:System.Globalization.CultureInfo parameter. This rule ignores calls to the following methods:
- xref:System.Activator.CreateInstance%2A?displayProperty=nameWithType
- xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=nameWithType
- xref:System.Resources.ResourceManager.GetString%2A?displayProperty=nameWithType
You can also configure more symbols to be excluded by this rule.
When a xref:System.Globalization.CultureInfo or xref:System.IFormatProvider?displayProperty=nameWithType object is not supplied, the default value that is supplied by the overloaded member might not have the effect that you want in all locales. Also, .NET members choose default culture and formatting based on assumptions that might not be correct for your code. To ensure the code works as expected for your scenarios, you should supply culture-specific information according to the following guidelines:
-
If the value will be displayed to the user, use the current culture. See xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType.
-
If the value will be stored and accessed by software, that is, persisted to a file or database, use the invariant culture. See xref:System.Globalization.CultureInfo.InvariantCulture%2A?displayProperty=nameWithType.
-
If you do not know the destination of the value, have the data consumer or provider specify the culture.
Even if the default behavior of the overloaded member is appropriate for your needs, it is better to explicitly call the culture-specific overload so that your code is self-documenting and more easily maintained.
Note
xref:System.Globalization.CultureInfo.CurrentUICulture%2A?displayProperty=nameWithType is used only to retrieve localized resources by using an instance of the xref:System.Resources.ResourceManager?displayProperty=nameWithType class.
To fix a violation of this rule, use the overload that takes a xref:System.Globalization.CultureInfo argument.
It is safe to suppress a warning from this rule when it is certain that the default culture is the correct choice, and where code maintainability is not an important development priority.
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 CA1304
// The code that's violating the rule is on this line.
#pragma warning restore CA1304
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA1304.severity = none
For more information, see How to suppress code analysis warnings.
Use the following options to configure which parts of your codebase to run this rule on.
You can configure these options for just this rule, for all rules it applies to, or for all rules in this category (Globalization) that it applies to. For more information, see Code quality rule configuration options.
[!INCLUDEexcluded-symbol-names]
[!INCLUDEexcluded-type-names-with-derived-types]
In the following example, BadMethod
causes two violations of this rule. GoodMethod
corrects the first violation by passing the invariant culture to xref:System.String.Compare%2A?displayProperty=nameWithType, and corrects the second violation by passing the current culture to xref:System.String.ToLower%2A?displayProperty=nameWithType because string3
is displayed to the user.
:::code language="csharp" source="snippets/csharp/all-rules/ca1304.cs" id="snippet1":::
The following example shows the effect of current culture on the default xref:System.IFormatProvider that is selected by the xref:System.DateTime type.
:::code language="csharp" source="snippets/csharp/all-rules/ca1304.cs" id="snippet2":::
This example produces the following output:
6/4/1900 12:15:12 PM
06/04/1900 12:15:12