CA1304: Specify CultureInfo
TypeName |
SpecifyCultureInfo |
CheckId |
CA1304 |
Category |
Microsoft.Globalization |
Breaking Change |
Non-breaking |
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 CultureInfo parameter. This rule ignores calls to the following methods:
Rule Description
When a 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 Framework 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 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 CultureInfo.InvariantCulture.
If you do not know the destination of the value, have the data consumer or provider specify the culture.
Note that CultureInfo.CurrentUICulture is used only to retrieve localized resources by using an instance of the System.Resources.ResourceManager class.
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.
How to Fix Violations
To fix a violation of this rule, use the overload that takes a CultureInfo or IFormatProvider and specify the argument according to the guidelines that were listed earlier.
When to Suppress Warnings
It is safe to suppress a warning from this rule when it is certain that the default culture/format provider is the correct choice, and where code maintainability is not an important development priority.
Example
In the following example, BadMethod causes two violations of this rule. GoodMethod corrects the first violation by passing the invariant culture to System.String.Compare, and corrects the second violation by passing the current culture to ToLower because string3 is displayed to the user.
using System;
using System.Globalization;
namespace GlobalizationLibrary
{
public class CultureInfoTest
{
public void BadMethod(String string1, String string2, String string3)
{
if(string.Compare(string1, string2, false) == 0)
{
Console.WriteLine(string3.ToLower());
}
}
public void GoodMethod(String string1, String string2, String string3)
{
if(string.Compare(string1, string2, false,
CultureInfo.InvariantCulture) == 0)
{
Console.WriteLine(string3.ToLower(CultureInfo.CurrentCulture));
}
}
}
}
The following example shows the effect of current culture on the default IFormatProvider that is selected by the DateTime type.
using System;
using System.Globalization;
using System.Threading;
namespace GlobalLibGlobalLibrary
{
public class IFormatProviderTest
{
public static void Main()
{
string dt = "6/4/1900 12:15:12";
// The default behavior of DateTime.Parse is to use
// the current culture.
// Violates rule: SpecifyIFormatProvider.
DateTime myDateTime = DateTime.Parse(dt);
Console.WriteLine(myDateTime);
// Change the current culture to the French culture,
// and parsing the same string yields a different value.
Thread.CurrentThread.CurrentCulture = new CultureInfo("Fr-fr", true);
myDateTime = DateTime.Parse(dt);
Console.WriteLine(myDateTime);
}
}
}
This example produces the following output.
6/4/1900 12:15:12 PM 06/04/1900 12:15:12
Related Rules
CA1305: Specify IFormatProvider