Specify CultureInfo
Specify CultureInfo
Microsoft docsDescription
When a System.Globalization.CultureInfo or System.IFormatProvider 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 System.Globalization.CultureInfo.CurrentCulture.
- If the value will be stored and accessed by software, that is, persisted to a file or database, use the invariant culture. See System.Globalization.CultureInfo.InvariantCulture.
- 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. System.Globalization.CultureInfo.CurrentUICulture is used only to retrieve localized resources by using an instance of the System.Resources.ResourceManager class.
Cause
A method or constructor calls a member that has an overload that accepts a System.Globalization.CultureInfo parameter, and the method or constructor does not call the overload that takes the System.Globalization.CultureInfo parameter. This rule ignores calls to the following methods:
- System.Activator.CreateInstance
- System.Resources.ResourceManager.GetObject
- System.Resources.ResourceManager.GetString
You can also configure more symbols to be excluded by this rule.
How to fix violations
To fix a violation of this rule, use the overload that takes a System.Globalization.CultureInfo argument.
Example
#pragma warning disable CA1304
// The code that's violating the rule is on this line.
#pragma warning restore CA1304When to suppress
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.