22using System ;
33using System . Globalization ;
44
5- public class Example
5+ public class UInt16ParseExample2
66{
7- public static void Main ( )
8- {
9- string [ ] values = { " 214 " , "1,064" , "(0)" , "1241+" , " + 214 " , " +214 " , "2153.0" , "1e03" , "1300.0e-2" } ;
10- NumberStyles whitespace = NumberStyles . AllowLeadingWhite | NumberStyles . AllowTrailingWhite ;
11- NumberStyles [ ] styles = { NumberStyles . None , whitespace ,
12- NumberStyles . AllowLeadingSign | NumberStyles . AllowTrailingSign | whitespace ,
13- NumberStyles . AllowThousands | NumberStyles . AllowCurrencySymbol ,
7+ public static void Run ( )
8+ {
9+ string [ ] values = { " 214 " , "1,064" , "(0)" , "1241+" , " + 214 " , " +214 " , "2153.0" , "1e03" , "1300.0e-2" } ;
10+ NumberStyles whitespace = NumberStyles . AllowLeadingWhite | NumberStyles . AllowTrailingWhite ;
11+ NumberStyles [ ] styles = { NumberStyles . None , whitespace ,
12+ NumberStyles . AllowLeadingSign | NumberStyles . AllowTrailingSign | whitespace ,
13+ NumberStyles . AllowThousands | NumberStyles . AllowCurrencySymbol ,
1414 NumberStyles . AllowExponent | NumberStyles . AllowDecimalPoint } ;
1515
16- // Attempt to convert each number using each style combination.
17- foreach ( string value in values )
18- {
19- Console . WriteLine ( "Attempting to convert '{0}':" , value ) ;
20- foreach ( NumberStyles style in styles )
21- {
22- try {
23- ushort number = UInt16 . Parse ( value , style ) ;
24- Console . WriteLine ( " {0}: {1}" , style , number ) ;
25- }
26- catch ( FormatException ) {
27- Console . WriteLine ( " {0}: Bad Format" , style ) ;
16+ // Attempt to convert each number using each style combination.
17+ foreach ( string value in values )
18+ {
19+ Console . WriteLine ( $ "Attempting to convert '{ value } ':") ;
20+ foreach ( NumberStyles style in styles )
21+ {
22+ try
23+ {
24+ ushort number = ushort . Parse ( value , style ) ;
25+ Console . WriteLine ( $ " { style } : { number } ") ;
26+ }
27+ catch ( FormatException )
28+ {
29+ Console . WriteLine ( $ " { style } : Bad Format") ;
30+ }
2831 }
29- }
30- Console . WriteLine ( ) ;
31- }
32- }
32+ Console . WriteLine ( ) ;
33+ }
34+ }
3335}
3436// The example display the following output:
3537// Attempting to convert ' 214 ':
@@ -38,56 +40,56 @@ public static void Main()
3840// Integer, AllowTrailingSign: 214
3941// AllowThousands, AllowCurrencySymbol: Bad Format
4042// AllowDecimalPoint, AllowExponent: Bad Format
41- //
43+ //
4244// Attempting to convert '1,064':
4345// None: Bad Format
4446// AllowLeadingWhite, AllowTrailingWhite: Bad Format
4547// Integer, AllowTrailingSign: Bad Format
4648// AllowThousands, AllowCurrencySymbol: 1064
4749// AllowDecimalPoint, AllowExponent: Bad Format
48- //
50+ //
4951// Attempting to convert '(0)':
5052// None: Bad Format
5153// AllowLeadingWhite, AllowTrailingWhite: Bad Format
5254// Integer, AllowTrailingSign: Bad Format
5355// AllowThousands, AllowCurrencySymbol: Bad Format
5456// AllowDecimalPoint, AllowExponent: Bad Format
55- //
57+ //
5658// Attempting to convert '1241+':
5759// None: Bad Format
5860// AllowLeadingWhite, AllowTrailingWhite: Bad Format
5961// Integer, AllowTrailingSign: 1241
6062// AllowThousands, AllowCurrencySymbol: Bad Format
6163// AllowDecimalPoint, AllowExponent: Bad Format
62- //
64+ //
6365// Attempting to convert ' + 214 ':
6466// None: Bad Format
6567// AllowLeadingWhite, AllowTrailingWhite: Bad Format
6668// Integer, AllowTrailingSign: Bad Format
6769// AllowThousands, AllowCurrencySymbol: Bad Format
6870// AllowDecimalPoint, AllowExponent: Bad Format
69- //
71+ //
7072// Attempting to convert ' +214 ':
7173// None: Bad Format
7274// AllowLeadingWhite, AllowTrailingWhite: Bad Format
7375// Integer, AllowTrailingSign: 214
7476// AllowThousands, AllowCurrencySymbol: Bad Format
7577// AllowDecimalPoint, AllowExponent: Bad Format
76- //
78+ //
7779// Attempting to convert '2153.0':
7880// None: Bad Format
7981// AllowLeadingWhite, AllowTrailingWhite: Bad Format
8082// Integer, AllowTrailingSign: Bad Format
8183// AllowThousands, AllowCurrencySymbol: Bad Format
8284// AllowDecimalPoint, AllowExponent: 2153
83- //
85+ //
8486// Attempting to convert '1e03':
8587// None: Bad Format
8688// AllowLeadingWhite, AllowTrailingWhite: Bad Format
8789// Integer, AllowTrailingSign: Bad Format
8890// AllowThousands, AllowCurrencySymbol: Bad Format
8991// AllowDecimalPoint, AllowExponent: 1000
90- //
92+ //
9193// Attempting to convert '1300.0e-2':
9294// None: Bad Format
9395// AllowLeadingWhite, AllowTrailingWhite: Bad Format
0 commit comments