You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A method or constructor calls a member that has an overload that accepts a <xref:System.Globalization.CultureInfo?displayProperty=fullName> parameter, and the method or constructor does not call the overload that takes the <xref:System.Globalization.CultureInfo> parameter. This rule ignores calls to the following methods:
A method or constructor calls a member that has an overload that accepts a <xref:System.Globalization.CultureInfo?displayProperty=nameWithType> parameter, and the method or constructor does not call the overload that takes the <xref:System.Globalization.CultureInfo> parameter. This rule ignores calls to the following methods:
When a <xref:System.Globalization.CultureInfo> or <xref:System.IFormatProvider?displayProperty=nameWithType> 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:
40
+
41
+
- If the value will be displayed to the user, use the current culture. See <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType>.
-If the value will be stored and accessed by software, that is, persisted to a file or database, use the invariant culture. See <xref:System.Globalization.CultureInfo.InvariantCulture%2A?displayProperty=nameWithType>.
-If you do not know the destination of the value, have the data consumer or provider specify the culture.
36
46
37
-
## Rule Description
38
-
When a <xref:System.Globalization.CultureInfo> or <xref:System.IFormatProvider?displayProperty=fullName> 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, [!INCLUDE[dnprdnshort](../code-quality/includes/dnprdnshort_md.md)] 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:
47
+
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.
39
48
40
-
- If the value will be displayed to the user, use the current culture. See <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=fullName>.
49
+
> [!NOTE]
50
+
> <xref:System.Globalization.CultureInfo.CurrentUICulture%2A?displayProperty=nameWithType> is used only to retrieve localized resources by using an instance of the <xref:System.Resources.ResourceManager?displayProperty=nameWithType> class.
41
51
42
-
- If the value will be stored and accessed by software, that is, persisted to a file or database, use the invariant culture. See <xref:System.Globalization.CultureInfo.InvariantCulture%2A?displayProperty=fullName>.
52
+
## How to fix violations
43
53
44
-
- If you do not know the destination of the value, have the data consumer or provider specify the culture.
54
+
To fix a violation of this rule, use the overload that takes a <xref:System.Globalization.CultureInfo> argument.
45
55
46
-
Note that <xref:System.Globalization.CultureInfo.CurrentUICulture%2A?displayProperty=fullName> is used only to retrieve localized resources by using an instance of the <xref:System.Resources.ResourceManager?displayProperty=fullName> class.
56
+
## When to suppress warnings
47
57
48
-
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.
58
+
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.
49
59
50
-
## How to Fix Violations
51
-
To fix a violation of this rule, use the overload that takes a <xref:System.Globalization.CultureInfo> or <xref:System.IFormatProvider> and specify the argument according to the guidelines that were listed earlier.
60
+
## Example showing how to fix violations
52
61
53
-
## When to Suppress Warnings
54
-
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.
62
+
In the following example, `BadMethod` causes two violations of this rule. `GoodMethod` corrects the first violation by passing the invariant culture to <xref:System.String.Compare%2A?displayProperty=nameWithType>, and corrects the second violation by passing the current culture to <xref:System.String.ToLower%2A?displayProperty=nameWithType> because `string3` is displayed to the user.
55
63
56
-
## Example
57
-
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 <xref:System.String.ToLower%2A> because `string3` is displayed to the user.
The following example shows the effect of current culture on the default <xref:System.IFormatProvider> that is selected by the <xref:System.DateTime> type.
68
+
The following example shows the effect of current culture on the default <xref:System.IFormatProvider> that is selected by the <xref:System.DateTime> type.
A method or constructor calls one or more members that have overloads that accept a <xref:System.IFormatProvider?displayProperty=fullName> parameter, and the method or constructor does not call the overload that takes the <xref:System.IFormatProvider> parameter. This rule ignores calls to [!INCLUDE[dnprdnshort](../code-quality/includes/dnprdnshort_md.md)] methods that are documented as ignoring the <xref:System.IFormatProvider> parameter and additionally the following methods:
A method or constructor calls one or more members that have overloads that accept a <xref:System.IFormatProvider?displayProperty=fullName> parameter, and the method or constructor does not call the overload that takes the <xref:System.IFormatProvider> parameter.
This rule ignores calls to .NET Framework methods that are documented as ignoring the <xref:System.IFormatProvider> parameter. The rule also ignores the following methods:
When a <xref:System.Globalization.CultureInfo?displayProperty=fullName> or <xref: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, [!INCLUDE[dnprdnshort](../code-quality/includes/dnprdnshort_md.md)] 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:
41
+
## Rule description
39
42
40
-
- If the value will be displayed to the user, use the current culture. See <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=fullName>.
43
+
When a <xref:System.Globalization.CultureInfo?displayProperty=nameWithType> or <xref: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 make sure that the code works as expected for your scenarios, you should supply culture-specific information according to the following guidelines:
41
44
42
-
-If the value will be stored and accessed by software (persisted to a file or database), use the invariant culture. See <xref:System.Globalization.CultureInfo.InvariantCulture%2A?displayProperty=fullName>.
45
+
- If the value will be displayed to the user, use the current culture. See <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType>.
43
46
44
-
-If you do not know the destination of the value, have the data consumer or provider specify the culture.
47
+
- If the value will be stored and accessed by software (persisted to a file or database), use the invariant culture. See <xref:System.Globalization.CultureInfo.InvariantCulture%2A?displayProperty=nameWithType>.
45
48
46
-
Note that <xref:System.Globalization.CultureInfo.CurrentUICulture%2A?displayProperty=fullName> is used only to retrieve localized resources by using an instance of the <xref:System.Resources.ResourceManager?displayProperty=fullName> class.
49
+
- If you do not know the destination of the value, have the data consumer or provider specify the culture.
47
50
48
-
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.
51
+
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.
49
52
50
-
## How to Fix Violations
51
-
To fix a violation of this rule, use the overload that takes a <xref:System.Globalization.CultureInfo> or <xref:System.IFormatProvider> and specify the argument according to the guidelines that were listed earlier.
53
+
## How to fix violations
52
54
53
-
## When to Suppress Warnings
54
-
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.
55
+
To fix a violation of this rule, use the overload that takes an <xref:System.IFormatProvider> argument. Or, use a [C# interpolated string](/dotnet/csharp/tutorials/string-interpolation) and pass it to the <xref:System.FormattableString.Invariant%2A?displayProperty=nameWithType> method.
55
56
56
-
## Example
57
-
In the following example, `BadMethod` causes two violations of this rule. `GoodMethod` corrects the first violation by passing the invariant culture to <xref:System.String.Compare%2A>, and corrects the second violation by passing the current culture to <xref:System.String.ToLower%2A> because `string3` is displayed to the user.
It is safe to suppress a warning from this rule when it is certain that the default format is the correct choice, and where code maintainability is not an important development priority.
60
60
61
61
## Example
62
-
The following example shows the effect of current culture on the default <xref:System.IFormatProvider> that is selected by the <xref:System.DateTime> type.
In the following code, the `example1` string violates rule CA1305. The `example2` string satisfies rule CA1305 by passing <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType>, which implements <xref:System.IFormatProvider>, to <xref:System.String.Format(System.IFormatProvider,System.String,System.Object)?displayProperty=nameWithType>. The `example3` string satisfies rule CA1305 by passing an interpolated string to <xref:System.FormattableString.Invariant%2A?displayProperty=fullName]>.
0 commit comments