Skip to content

Commit 1795d42

Browse files
authored
Add snippets (dotnet#7473)
1 parent 18664fb commit 1795d42

File tree

15 files changed

+543
-0
lines changed

15 files changed

+543
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="int16_equals.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// System.Int16.Equals(Object)
2+
3+
(*
4+
The following program demonstrates the 'Equals(Object)' method
5+
of struct 'Int16'. This compares an instance of 'Int16' with the
6+
passed in object and returns true if they are equal.
7+
*)
8+
9+
// <Snippet1>
10+
let myVariable1 = 20s
11+
let myVariable2 = 20s
12+
13+
// Get and display the declaring type.
14+
printfn $"\nType of 'myVariable1' is '{myVariable1.GetType()}' and value is: {myVariable1}"
15+
printfn $"\nType of 'myVariable1' is '{myVariable2.GetType()}' and value is: {myVariable2}"
16+
17+
// Compare 'myVariable1' instance with 'myVariable2' Object.
18+
if myVariable1.Equals myVariable2 then
19+
printfn "\nStructures 'myVariable1' and 'myVariable2' are equal"
20+
else
21+
printfn "\nStructures 'myVariable1' and 'myVariable2' are not equal"
22+
23+
// </Snippet1>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// <Snippet1>
2+
open System
3+
4+
let numbersToConvert = [ 162345L; 32183L; -54000L ]
5+
6+
for number in numbersToConvert do
7+
if number >= int64 Int16.MinValue && number <= int64 Int16.MaxValue then
8+
let newNumber = Convert.ToInt16 number
9+
printfn $"Successfully converted {newNumber} to an Int16."
10+
else
11+
printfn $"Unable to convert {number} to an Int16."
12+
13+
// The example displays the following output to the console:
14+
// Unable to convert 162345 to an Int16.
15+
// Successfully converted 32183 to an Int16.
16+
// Unable to convert -54000 to an Int16.
17+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="MaxValue.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
module Parse
2+
3+
open System
4+
open System.Globalization
5+
6+
7+
let callParse1 () =
8+
// <Snippet1>
9+
let value = " 12603 "
10+
try
11+
let number = Int16.Parse value
12+
printfn $"Converted '{value}' to {number}."
13+
with :? FormatException ->
14+
printfn $"Unable to convert '{value}' to a 16-bit signed integer."
15+
16+
let value = " 16,054"
17+
try
18+
let number = Int16.Parse value
19+
printfn $"Converted '{value}' to {number}."
20+
with :? FormatException ->
21+
printfn "Unable to convert '{value}' to a 16-bit signed integer."
22+
23+
let value = " -17264"
24+
try
25+
let number = Int16.Parse value
26+
printfn $"Converted '{value}' to {number}."
27+
with :? FormatException ->
28+
printfn "Unable to convert '{value}' to a 16-bit signed integer."
29+
30+
31+
// The example displays the following output to the console:
32+
// Converted ' 12603 ' to 12603.
33+
// Unable to convert ' 16,054' to a 16-bit signed integer.
34+
// Converted ' -17264' to -17264.
35+
// </Snippet1>
36+
37+
let callParse3 () =
38+
// <Snippet3>
39+
// Parse string using "." as the thousands separator
40+
// and " " as the decimal separator.
41+
let value = "19 694,00"
42+
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
43+
let provider = CultureInfo "fr-FR"
44+
45+
let number = Int16.Parse(value, style, provider)
46+
printfn $"'{value}' converted to {number}."
47+
// Displays:
48+
// '19 694,00' converted to 19694.
49+
50+
try
51+
let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
52+
printfn $"'{value}' converted to {number}."
53+
with :? FormatException ->
54+
printfn $"Unable to parse '{value}'."
55+
// Displays:
56+
// Unable to parse '19 694,00'.
57+
58+
// Parse string using "$" as the currency symbol for en_GB and
59+
// en-US cultures.
60+
let value = "$6,032.00"
61+
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
62+
63+
try
64+
let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
65+
printfn $"'{value}' converted to {number}."
66+
with :? FormatException ->
67+
printfn $"Unable to parse '{value}'."
68+
// Displays:
69+
// Unable to parse '$6,032.00'.
70+
71+
let provider = CultureInfo "en-US"
72+
let number = Int16.Parse(value, style, provider)
73+
printfn $"'{value}' converted to {number}."
74+
// Displays:
75+
// '$6,032.00' converted to 6032.
76+
// </Snippet3>
77+
78+
79+
let callParse4 () =
80+
// <Snippet4>
81+
let stringToConvert = " 214 "
82+
try
83+
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
84+
printfn $"Converted '{stringToConvert}' to {number}."
85+
with
86+
| :? FormatException ->
87+
printfn $"Unable to parse '{stringToConvert}'."
88+
| :? OverflowException ->
89+
printfn $"'{stringToConvert}' is out of range of the Int16 data type."
90+
91+
let stringToConvert = " + 214"
92+
try
93+
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
94+
printfn $"Converted '{stringToConvert}' to {number}."
95+
with
96+
| :? FormatException ->
97+
printfn $"Unable to parse '{stringToConvert}'."
98+
| :? OverflowException ->
99+
printfn $"'{stringToConvert}' is out of range of the Int16 data type."
100+
101+
let stringToConvert = " +214 "
102+
try
103+
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
104+
printfn $"Converted '{stringToConvert}' to {number}."
105+
with
106+
| :? FormatException ->
107+
printfn $"Unable to parse '{stringToConvert}'."
108+
| :? OverflowException ->
109+
printfn $"'{stringToConvert}' is out of range of the Int16 data type."
110+
111+
// The example displays the following output to the console:
112+
// Converted ' 214 ' to 214.
113+
// Unable to parse ' + 214'.
114+
// Converted ' +214 ' to 214.
115+
// </Snippet4>
116+
117+
118+
callParse1 ()
119+
printfn "-----"
120+
callParse3 ()
121+
printfn "-----"
122+
callParse4 ()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// <Snippet2>
2+
open System
3+
open System.Globalization
4+
5+
let parseToInt16 (value: string) (style: NumberStyles) =
6+
try
7+
let number = Int16.Parse(value, style)
8+
printfn $"Converted '{value}' to {number}."
9+
with
10+
| :? FormatException ->
11+
printfn $"Unable to parse '{value}' with style {style}."
12+
| :? OverflowException ->
13+
printfn $"'{value}' is out of range of the Int16 type."
14+
15+
[<EntryPoint>]
16+
let main _ =
17+
// Parse a number with a thousands separator (throws an exception).
18+
let value = "14,644"
19+
let style = NumberStyles.None
20+
parseToInt16 value style
21+
22+
let style = NumberStyles.AllowThousands
23+
parseToInt16 value style
24+
25+
// Parse a number with a thousands separator and decimal point.
26+
let value = "14,644.00"
27+
let style = NumberStyles.AllowThousands ||| NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
28+
parseToInt16 value style
29+
30+
// Parse a number with a fractional component (throws an exception).
31+
let value = "14,644.001"
32+
parseToInt16 value style
33+
34+
// Parse a number in exponential notation.
35+
let value = "145E02"
36+
let style = style ||| NumberStyles.AllowExponent
37+
parseToInt16 value style
38+
39+
// Parse a number in exponential notation with a positive sign.
40+
let value = "145E+02"
41+
parseToInt16 value style
42+
43+
// Parse a number in exponential notation with a negative sign
44+
// (throws an exception).
45+
let value = "145E-02"
46+
parseToInt16 value style
47+
48+
0
49+
50+
// The example displays the following output to the console:
51+
// Unable to parse '14,644' with style None.
52+
// Converted '14,644' to 14644.
53+
// Converted '14,644.00' to 14644.
54+
// '14,644.001' is out of range of the Int16 type.
55+
// Converted '145E02' to 14500.
56+
// Converted '145E+02' to 14500.
57+
// '145E-02' is out of range of the Int16 type.
58+
// </Snippet2>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Parse.fs" />
8+
<Compile Include="Parse2.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
open System
2+
open System.Globalization
3+
4+
let callDefaultToString () =
5+
// <Snippet1>
6+
let numbers = [ 0s; 14624s; 13982s; Int16.MaxValue; Int16.MinValue; -16667s ]
7+
8+
for number in numbers do
9+
printfn $"{number.ToString()}"
10+
11+
// The example displays the following output to the console:
12+
// 0
13+
// 14624
14+
// 13982
15+
// 32767
16+
// -32768
17+
// -16667
18+
// </Snippet1>
19+
20+
let callToStringWithProvider () =
21+
// <Snippet2>
22+
let numbers = [ -23092s; 0s; 14894s; Int16.MaxValue ]
23+
let providers =
24+
[ CultureInfo "en-us"
25+
CultureInfo "fr-fr"
26+
CultureInfo "de-de"
27+
CultureInfo "es-es" ]
28+
29+
for int16Value in numbers do
30+
for provider in providers do
31+
printf $"{int16Value.ToString provider, 6} ({provider.Name}) "
32+
33+
printfn ""
34+
35+
// The example displays the following output to the console:
36+
// -23092 (en-US) -23092 (fr-FR) -23092 (de-DE) -23092 (es-ES)
37+
// 0 (en-US) 0 (fr-FR) 0 (de-DE) 0 (es-ES)
38+
// 14894 (en-US) 14894 (fr-FR) 14894 (de-DE) 14894 (es-ES)
39+
// 32767 (en-US) 32767 (fr-FR) 32767 (de-DE) 32767 (es-ES)
40+
// </Snippet2>
41+
42+
let callToStringWithSpecifiers () =
43+
// <Snippet3>
44+
let values = [| -23805s; 32194s |]
45+
let formats =
46+
[ "C4"; "D6"; "e1"; "E2"; "F1"; "G"; "N1"
47+
"P0"; "X4"; "000000.0000"; "##000.0" ]
48+
for format in formats do
49+
printfn $"'{format,2}' format specifier: {values[0].ToString format,17} {values[1].ToString format,17}"
50+
51+
// The example displays the following output to the console:
52+
// 'C4' format specifier: ($23,805.0000) $32,194.0000
53+
// 'D6' format specifier: -023805 032194
54+
// 'e1' format specifier: -2.4e+004 3.2e+004
55+
// 'E2' format specifier: -2.38E+004 3.22E+004
56+
// 'F1' format specifier: -23805.0 32194.0
57+
// ' G' format specifier: -23805 32194
58+
// 'N1' format specifier: -23,805.0 32,194.0
59+
// 'P0' format specifier: -2,380,500 % 3,219,400 %
60+
// 'X4' format specifier: A303 7DC2
61+
// '000000.0000' format specifier: -023805.0000 032194.0000
62+
// '##000.0' format specifier: -23805.0 32194.0
63+
// </Snippet3>
64+
65+
let callToStringWithSpecifiersAndProviders () =
66+
// <Snippet4>
67+
let value = 14603
68+
let formats =
69+
[ "C"; "D6"; "e1"; "E2"; "F1"; "G"; "N1"
70+
"P0"; "X4"; "000000.0000"; "##000.0" ]
71+
let providers =
72+
[ CultureInfo "en-us"
73+
CultureInfo "fr-fr"
74+
CultureInfo "de-de"
75+
CultureInfo "es-es" ]
76+
// Display header.
77+
printfn $"{providers[0],24}{providers[1],14}{providers[2],14}{providers[3],14}\n"
78+
79+
// Display a value using each format string.
80+
for format in formats do
81+
// Display the value for each provider on the same line.
82+
printf $"{format,-12}"
83+
for provider in providers do
84+
printf $"{value.ToString(format, provider),12} "
85+
printfn ""
86+
87+
// The example displays the following output to the console:
88+
// en-US fr-FR de-DE es-ES
89+
//
90+
// C $14,603.00 14 603,00 € 14.603,00 € 14.603,00 €
91+
// D6 014603 014603 014603 014603
92+
// e1 1.5e+004 1,5e+004 1,5e+004 1,5e+004
93+
// E2 1.46E+004 1,46E+004 1,46E+004 1,46E+004
94+
// F1 14603.0 14603,0 14603,0 14603,0
95+
// G 14603 14603 14603 14603
96+
// N1 14,603.0 14 603,0 14.603,0 14.603,0
97+
// P0 1,460,300 % 1 460 300 % 1.460.300% 1.460.300 %
98+
// X4 390B 390B 390B 390B
99+
// 000000.0000 014603.0000 014603,0000 014603,0000 014603,0000
100+
// ##000.0 14603.0 14603,0 14603,0 14603,0
101+
// </Snippet4>
102+
103+
callDefaultToString ()
104+
printfn "------"
105+
callToStringWithProvider ()
106+
printfn "------"
107+
callToStringWithSpecifiers ()
108+
printfn "------"
109+
callToStringWithSpecifiersAndProviders ()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="ToString1.fs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)