Export (0) Print
Expand All

CA1305: Specify IFormatProvider

TypeName

SpecifyIFormatProvider

CheckId

CA1305

Category

Microsoft.Globalization

Breaking Change

Non-breaking

A method or constructor calls one or more members that have overloads that accept a System.IFormatProvider parameter, and the method or constructor does not call the overload that takes the IFormatProvider parameter. This rule ignores calls to .NET Framework methods that are documented as ignoring the IFormatProvider parameter and additionally the following methods:

When a System.Globalization.CultureInfo or 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 make sure that 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 (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.

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.

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.

In the following example, BadMethod causes two violations of this rule. GoodMethod corrects the first violation by passing the invariant culture to 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

Community Additions

ADD
Show:
© 2014 Microsoft