Skip to content

Commit 24d586f

Browse files
committed
fixes #939
1 parent 5393646 commit 24d586f

File tree

2 files changed

+80
-56
lines changed

2 files changed

+80
-56
lines changed
Lines changed: 40 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,60 @@ 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+
35+
- <xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=nameWithType>
36+
37+
- <xref:System.Resources.ResourceManager.GetString%2A?displayProperty=nameWithType>
38+
39+
## Rule description
40+
41+
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:
3242

33-
- <xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=fullName>
43+
- If the value will be displayed to the user, use the current culture. See <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType>.
3444

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

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>.
51+
> [!NOTE]
52+
> <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.
4353
44-
- If you do not know the destination of the value, have the data consumer or provider specify the culture.
54+
## How to fix violations
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+
To fix a violation of this rule, use the overload that takes a <xref:System.Globalization.CultureInfo> argument.
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+
## When to suppress warnings
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+
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.
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+
## Example showing how to fix violations
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+
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.
5865

59-
[!code-csharp[FxCop.Globalization.CultureInfo#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_1.cs)]
66+
[!code-csharp[FxCop.Globalization.CultureInfo#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_1.cs)]
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+
## Example showing formatted output
6369

64-
[!code-csharp[FxCop.Globalization.IFormatProvider#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_2.cs)]
70+
The following example shows the effect of current culture on the default <xref:System.IFormatProvider> that is selected by the <xref:System.DateTime> type.
6571

66-
This example produces the following output.
72+
[!code-csharp[FxCop.Globalization.IFormatProvider#1](../code-quality/codesnippet/CSharp/ca1304-specify-cultureinfo_2.cs)]
73+
74+
This example produces the following output:
75+
76+
**6/4/1900 12:15:12 PM**
6777

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

73-
## See Also
74-
[Using the CultureInfo Class](/dotnet/standard/globalization-localization/globalization#Cultures)
80+
## Related rules
81+
82+
- [CA1305: Specify IFormatProvider](../code-quality/ca1305-specify-iformatprovider.md)
83+
84+
## See also
85+
86+
- [Using the CultureInfo Class](/dotnet/standard/globalization-localization/globalization#Cultures)
Lines changed: 40 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,58 @@ 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>
3638

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:
39+
- <xref:System.Resources.ResourceManager.GetObject%2A?displayProperty=nameWithType>
3940

40-
- If the value will be displayed to the user, use the current culture. See <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=fullName>.
41+
- <xref:System.Resources.ResourceManager.GetString%2A?displayProperty=nameWithType>
4142

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>.
43+
## Rule description
4344

44-
- If you do not know the destination of the value, have the data consumer or provider specify the culture.
45+
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:
4546

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.
47+
- If the value will be displayed to the user, use the current culture. See <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType>.
4748

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.
49+
- 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>.
4950

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.
51+
- If you do not know the destination of the value, have the data consumer or provider specify the culture.
5252

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.
53+
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.
5554

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.
55+
## How to fix violations
56+
57+
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.
5858

59-
[!code-csharp[FxCop.Globalization.CultureInfo#1](../code-quality/codesnippet/CSharp/ca1305-specify-iformatprovider_1.cs)]
59+
## When to suppress warnings
60+
61+
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.
6062

6163
## 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.
6364

64-
[!code-csharp[FxCop.Globalization.IFormatProvider#1](../code-quality/codesnippet/CSharp/ca1305-specify-iformatprovider_2.cs)]
65+
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)>. The `example3` string satisfies rule CA1305 by passing an interpolated string to <xref:System.FormattableString.Invariant%2A?displayProperty=nameWithType]>.
66+
67+
```csharp
68+
string name = "Georgette";
69+
70+
// Violates CA1305
71+
string example1 = String.Format("Hello {0}", name);
72+
73+
// Satisfies CA1305
74+
string example2 = String.Format(CultureInfo.CurrentCulture, "Hello {0}", name);
75+
76+
// Satisfies CA1305
77+
string example3 = FormattableString.Invariant($"Hello {name}");
78+
```
79+
80+
## Related rules
6581

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

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)
84+
## See also
7285

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

0 commit comments

Comments
 (0)