Skip to content

Commit 67a01e4

Browse files
authored
Merge pull request #2406 from gewarren/ca-0629
Clarifies CA1305 rule
2 parents facd030 + 862e4f3 commit 67a01e4

File tree

2 files changed

+76
-56
lines changed

2 files changed

+76
-56
lines changed
Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA1304: Specify CultureInfo"
3-
ms.date: 11/04/2016
3+
ms.date: 06/30/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: reference
@@ -18,6 +18,7 @@ ms.workload:
1818
- "multiple"
1919
---
2020
# CA1304: Specify CultureInfo
21+
2122
|||
2223
|-|-|
2324
|TypeName|SpecifyCultureInfo|
@@ -26,49 +27,58 @@ ms.workload:
2627
|Breaking Change|Non-breaking|
2728

2829
## Cause
29-
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:
3030

31-
- <xref:System.Activator.CreateInstance%2A?displayProperty=fullName>
31+
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:
32+
33+
- <xref:System.Activator.CreateInstance%2A?displayProperty=nameWithType>
34+
- <xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=nameWithType>
35+
- <xref:System.Resources.ResourceManager.GetString%2A?displayProperty=nameWithType>
36+
37+
## Rule description
38+
39+
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>.
3242

33-
- <xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=fullName>
43+
- 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>.
3444

35-
- <xref:System.Resources.ResourceManager.GetString%2A?displayProperty=fullName>
45+
- If you do not know the destination of the value, have the data consumer or provider specify the culture.
3646

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.
3948

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.
4151
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
4353

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.
4555

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
4757

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.
4959

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
5261

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.
5563

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.
64+
[!code-csharp[FxCop.Globalization.CultureInfo#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_1.cs)]
5865

59-
[!code-csharp[FxCop.Globalization.CultureInfo#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_1.cs)]
66+
## Example showing formatted output
6067

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.
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.
6369

64-
[!code-csharp[FxCop.Globalization.IFormatProvider#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_2.cs)]
70+
[!code-csharp[FxCop.Globalization.IFormatProvider#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_2.cs)]
6571

66-
This example produces the following output.
72+
This example produces the following output:
73+
74+
**6/4/1900 12:15:12 PM**
6775

68-
**6/4/1900 12:15:12 PM**
6976
**06/04/1900 12:15:12**
70-
## Related Rules
71-
[CA1305: Specify IFormatProvider](../code-quality/ca1305-specify-iformatprovider.md)
7277

73-
## See Also
74-
[Using the CultureInfo Class](/dotnet/standard/globalization-localization/globalization#Cultures)
78+
## Related rules
79+
80+
- [CA1305: Specify IFormatProvider](../code-quality/ca1305-specify-iformatprovider.md)
81+
82+
## See also
83+
84+
- [Using the CultureInfo Class](/dotnet/standard/globalization-localization/globalization#Cultures)
Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA1305: Specify IFormatProvider"
3-
ms.date: 11/04/2016
3+
ms.date: 06/30/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: reference
@@ -14,10 +14,13 @@ ms.assetid: fb34ed9a-4eab-47cc-8eef-3068a4a1397e
1414
author: gewarren
1515
ms.author: gewarren
1616
manager: douge
17+
dev_langs:
18+
- CSharp
1719
ms.workload:
1820
- "multiple"
1921
---
2022
# CA1305: Specify IFormatProvider
23+
2124
|||
2225
|-|-|
2326
|TypeName|SpecifyIFormatProvider|
@@ -26,49 +29,56 @@ ms.workload:
2629
|Breaking Change|Non-breaking|
2730

2831
## Cause
29-
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:
3032

31-
- <xref:System.Activator.CreateInstance%2A?displayProperty=fullName>
33+
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.
3234

33-
- <xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=fullName>
35+
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:
3436

35-
- <xref:System.Resources.ResourceManager.GetString%2A?displayProperty=fullName>
37+
- <xref:System.Activator.CreateInstance%2A?displayProperty=nameWithType>
38+
- <xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=nameWithType>
39+
- <xref:System.Resources.ResourceManager.GetString%2A?displayProperty=nameWithType>
3640

37-
## Rule Description
38-
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
3942

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

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>.
4346

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>.
4548

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.
4750

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.
4952

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
5254

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.
5556

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.
57+
## When to suppress warnings
5858

59-
[!code-csharp[FxCop.Globalization.CultureInfo#1](../code-quality/codesnippet/CSharp/ca1305-specify-iformatprovider_1.cs)]
59+
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.
6060

6161
## 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.
6362

64-
[!code-csharp[FxCop.Globalization.IFormatProvider#1](../code-quality/codesnippet/CSharp/ca1305-specify-iformatprovider_2.cs)]
63+
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]>.
64+
65+
```csharp
66+
string name = "Georgette";
67+
68+
// Violates CA1305
69+
string example1 = String.Format("Hello {0}", name);
70+
71+
// Satisfies CA1305
72+
string example2 = String.Format(CultureInfo.CurrentCulture, "Hello {0}", name);
73+
74+
// Satisfies CA1305
75+
string example3 = FormattableString.Invariant($"Hello {name}");
76+
```
77+
78+
## Related rules
6579

66-
This example produces the following output.
80+
- [CA1304: Specify CultureInfo](../code-quality/ca1304-specify-cultureinfo.md)
6781

68-
**6/4/1900 12:15:12 PM**
69-
**06/04/1900 12:15:12**
70-
## Related Rules
71-
[CA1304: Specify CultureInfo](../code-quality/ca1304-specify-cultureinfo.md)
82+
## See also
7283

73-
## See Also
74-
[Using the CultureInfo Class](/dotnet/standard/globalization-localization/globalization#Cultures)
84+
- [Using the CultureInfo Class](/dotnet/standard/globalization-localization/globalization#Cultures)

0 commit comments

Comments
 (0)