Export (0) Print
Expand All

Specify CultureInfo

TypeName

SpecifyCultureInfo

CheckId

CA1304

Category

Microsoft.Globalization

Breaking Change

NonBreaking

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:

When a CultureInfo or System.IFormatProvider object is not supplied, the default value 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:

Note that System.Globalization.CultureInfo.CurrentUICulture is used only to retrieve localized resources 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.

To fix a violation of this rule, use the overload that takes a CultureInfo or IFormatProvider, and specify the argument according to the guidelines listed earlier.

It is safe to exclude 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.

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 being 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 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:

Output

6/4/1900 12:15:12 PM
06/04/1900 12:15:12

Community Additions

ADD
Show:
© 2014 Microsoft