@@ -35,7 +35,7 @@ public class Currency : IFormattable
35
35
public Money MinorUnit => new Money ( new decimal ( 1 , 0 , 0 , false , ( byte ) DecimalDigits ) , this ) ;
36
36
37
37
/// <summary>
38
- /// Gets a list containing language identifers and currency names.
38
+ /// Gets a list containing language identifiers and currency names.
39
39
/// </summary>
40
40
public IEnumerable < ( string CultureName , string Name ) > LocalizedNames => _localizedNameLookup ? . Select ( kvp => ( kvp . Key , kvp . Value ) ) ?? Array . Empty < ( string , string ) > ( ) ;
41
41
@@ -54,7 +54,7 @@ public class Currency : IFormattable
54
54
/// is not found.
55
55
/// </remarks>
56
56
public Currency ( string name , string currencyCode , string symbol , int decimalDigits , params ( string CultureName , string Name ) [ ] localizedNames )
57
- : this ( name , currencyCode , symbol , decimalDigits , localizedNames . Length == 0 ? null : localizedNames . AsEnumerable ( ) ) { }
57
+ : this ( name , currencyCode , symbol , decimalDigits , localizedNames . Length is 0 ? null : localizedNames . AsEnumerable ( ) ) { }
58
58
59
59
/// <inheritdoc cref="Currency(string, string, string, int, ValueTuple{string, string}[])"/>
60
60
public Currency ( string name , string currencyCode , string symbol , int decimalDigits , IEnumerable < ( string CultureName , string Name ) > ? localizedNames = null )
@@ -63,16 +63,16 @@ public Currency(string name, string currencyCode, string symbol, int decimalDigi
63
63
name = name . Trim ( ) ;
64
64
symbol = symbol . Trim ( ) ;
65
65
66
- if ( currencyCode . Length == 0 )
66
+ if ( currencyCode . Length is 0 )
67
67
throw new ArgumentException ( "Currency code is required." , nameof ( currencyCode ) ) ;
68
68
69
69
if ( currencyCode . Length > 20 )
70
70
throw new ArgumentOutOfRangeException ( nameof ( currencyCode ) , "Currency code has a maximum length of 20 characters." ) ;
71
71
72
- if ( name . Length == 0 )
72
+ if ( name . Length is 0 )
73
73
throw new ArgumentException ( "Name is required." , nameof ( name ) ) ;
74
74
75
- if ( symbol . Length == 0 )
75
+ if ( symbol . Length is 0 )
76
76
throw new ArgumentException ( "Symbol is required." , nameof ( symbol ) ) ;
77
77
78
78
if ( symbol . Length > 20 )
@@ -86,14 +86,23 @@ public Currency(string name, string currencyCode, string symbol, int decimalDigi
86
86
Symbol = symbol ;
87
87
DecimalDigits = decimalDigits ;
88
88
89
- if ( localizedNames != null )
89
+ if ( localizedNames is not null )
90
90
{
91
- foreach ( var ( cultureName , localName ) in localizedNames )
91
+ foreach ( var localizedName in localizedNames )
92
92
{
93
+ string cultureName = CoerceCultureName ( localizedName . CultureName ) ;
94
+ string localName = CoerceCurrencyName ( localizedName . Name ) ;
95
+
93
96
_localizedNameLookup ??= new ( StringComparer . OrdinalIgnoreCase ) ;
97
+ #if NETSTANDARD
98
+ if ( _localizedNameLookup . ContainsKey ( cultureName ) )
99
+ throw new ArgumentException ( $ "Duplicate culture name '{ cultureName } ' in localized names.", nameof ( localizedNames ) ) ;
94
100
95
- if ( ! _localizedNameLookup . TryAdd ( CoerceCultureName ( cultureName ) , CoerceCurrencyName ( localName ) ) )
101
+ _localizedNameLookup . Add ( cultureName , localName ) ;
102
+ #else
103
+ if ( ! _localizedNameLookup . TryAdd ( cultureName , localName ) )
96
104
throw new ArgumentException ( $ "Duplicate culture name '{ cultureName } ' in localized names.", nameof ( localizedNames ) ) ;
105
+ #endif
97
106
}
98
107
}
99
108
@@ -159,15 +168,15 @@ public string ToString(string? format, CultureInfo? culture = null)
159
168
160
169
string name ;
161
170
162
- if ( _localizedNameLookup == null )
171
+ if ( _localizedNameLookup is null )
163
172
{
164
173
name = Name ;
165
174
}
166
175
else if ( ! _localizedNameLookup . TryGetValue ( culture . Name , out name ) )
167
176
{
168
177
var neutralCulture = culture . GetNeutralCulture ( ) ;
169
178
170
- if ( neutralCulture == null || ! _localizedNameLookup . TryGetValue ( neutralCulture . Name , out name ) )
179
+ if ( neutralCulture is null || ! _localizedNameLookup . TryGetValue ( neutralCulture . Name , out name ) )
171
180
name = Name ;
172
181
}
173
182
@@ -220,9 +229,13 @@ internal static CurrencyRegistry CreateSystemRegistry()
220
229
221
230
if ( localizedNameLookup . TryGetValue ( localizationCultureName , out string existingLocalizedName ) && existingLocalizedName != localizedName )
222
231
{
223
- // This shouldn't happen, but if the data changes and it does then make this future-proof by adding the localized name to the specific
224
- // culture instead to override the different neutral culture name that was set.
232
+ // This shouldn't happen in .NET+, but if the data changes and it does then make this future-proof by adding the localized name to the
233
+ // specific culture instead to override the different neutral culture name that was set.
234
+
235
+ // This *does* happen on .NET Framework, so neutral culture name will be whatever specific culture name was first.
236
+ #if ! NETSTANDARD
225
237
Debug . Fail ( "Neutral localization culture name was already set to a different name." ) ;
238
+ #endif
226
239
localizedNameLookup . Add ( culture . Name , localizedName ) ;
227
240
}
228
241
else
0 commit comments