Partilhar via


CA1305: Specify IFormatProvider

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName SpecifyIFormatProvider
CheckId CA1305
Category Microsoft.Globalization
Breaking Change Non-breaking

Cause

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:

Rule Description

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:

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 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));
            }
        }
    }
}

Example

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

CA1304: Specify CultureInfo

See Also

NIB: Using the CultureInfo Class