Skip to content

Commit 42a56bd

Browse files
authored
System.Byte F# snippets (#7532)
* System.Byte F# snippets * Update Byte.xml
1 parent cd08a43 commit 42a56bd

File tree

21 files changed

+640
-2
lines changed

21 files changed

+640
-2
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="systembyte.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
open System
2+
3+
//<Snippet1>
4+
let minMaxFields numberToSet =
5+
if numberToSet <= int Byte.MaxValue && numberToSet >= int Byte.MinValue then
6+
// You must explicitly convert an integer to a byte.
7+
let myByte = byte numberToSet
8+
9+
printfn $"The MemberByte value is {myByte}"
10+
else
11+
printfn $"The value {numberToSet} is outside of the range of possible Byte values"
12+
13+
//</Snippet1>
14+
15+
//<Snippet3>
16+
let compare (byte1: byte) byte2 =
17+
let myCompareResult = byte1.CompareTo byte2
18+
19+
if myCompareResult > 0 then
20+
printfn $"{byte1} is less than the MemberByte value {byte2}"
21+
22+
elif myCompareResult < 0 then
23+
printfn $"{byte1} is greater than the MemberByte value {byte2}"
24+
25+
else
26+
printfn $"{byte1} is equal to the MemberByte value {byte2}"
27+
//</Snippet3>
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="parse.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
open System
2+
open System.Globalization
3+
4+
let callParse1 () =
5+
// <Snippet1>
6+
let stringToConvert = " 162"
7+
try
8+
let byteValue = Byte.Parse stringToConvert
9+
printfn $"Converted '{stringToConvert}' to {byteValue}."
10+
with
11+
| :? FormatException ->
12+
printfn $"Unable to parse '{stringToConvert}'."
13+
| :? OverflowException ->
14+
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."
15+
16+
// The example displays the following output to the console:
17+
// Converted ' 162' to 162.
18+
// </Snippet1>
19+
20+
let callParse2 () =
21+
// <Snippet2>
22+
let stringToConvert = " 214 "
23+
try
24+
let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
25+
printfn $"Converted '{stringToConvert}' to {byteValue}."
26+
with
27+
| :? FormatException ->
28+
printfn $"Unable to parse '{stringToConvert}'."
29+
| :? OverflowException ->
30+
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."
31+
32+
let stringToConvert = " + 214 "
33+
try
34+
let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
35+
printfn $"Converted '{stringToConvert}' to {byteValue}."
36+
with
37+
| :? FormatException ->
38+
printfn $"Unable to parse '{stringToConvert}'."
39+
| :? OverflowException ->
40+
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."
41+
42+
let stringToConvert = " +214 "
43+
try
44+
let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
45+
printfn $"Converted '{stringToConvert}' to {byteValue}."
46+
with
47+
| :? FormatException ->
48+
printfn $"Unable to parse '{stringToConvert}'."
49+
| :? OverflowException ->
50+
printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."
51+
52+
// The example displays the following output to the console:
53+
// Converted ' 214 ' to 214.
54+
// Unable to parse ' + 214 '.
55+
// Converted ' +214 ' to 214.
56+
// </Snippet2>
57+
58+
let callParse3 () =
59+
// <Snippet3>
60+
// Parse value with no styles allowed.
61+
let style = NumberStyles.None
62+
let value = " 241 "
63+
try
64+
let number = Byte.Parse(value, style);
65+
printfn $"Converted '{value}' to {number}."
66+
with :? FormatException ->
67+
printfn $"Unable to parse '{value}'."
68+
69+
// Parse value with trailing sign.
70+
let style = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
71+
let value = " 163+"
72+
let number = Byte.Parse(value, style)
73+
printfn $"Converted '{value}' to {number}."
74+
75+
// Parse value with leading sign.
76+
let value = " +253 "
77+
let number = Byte.Parse(value, style)
78+
printfn $"Converted '{value}' to {number}."
79+
80+
// This example displays the following output to the console:
81+
// Unable to parse ' 241 '.
82+
// Converted ' 163+' to 163.
83+
// Converted ' +253 ' to 253.
84+
// </Snippet3>
85+
86+
let callParse4 () =
87+
// <Snippet4>
88+
// Parse number with decimals.
89+
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
90+
let style = NumberStyles.Float
91+
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
92+
let value = "12,000"
93+
94+
let number = Byte.Parse(value, style, culture)
95+
printfn $"Converted '{value}' to {number}."
96+
97+
let culture = CultureInfo.CreateSpecificCulture "en-GB"
98+
try
99+
let number = Byte.Parse(value, style, culture)
100+
printfn $"Converted '{value}' to {number}."
101+
with :? FormatException ->
102+
printfn $"Unable to parse '{value}'."
103+
104+
let value = "12.000"
105+
let number = Byte.Parse(value, style, culture)
106+
printfn $"Converted '{value}' to {number}."
107+
108+
// The example displays the following output to the console:
109+
// Converted '12,000' to 12.
110+
// Unable to parse '12,000'.
111+
// Converted '12.000' to 12.
112+
// </Snippet4>
113+
114+
callParse1 ()
115+
printfn ""
116+
callParse2 ()
117+
printfn ""
118+
callParse3 ()
119+
printfn ""
120+
callParse4 ()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
open System
2+
open System.Globalization
3+
4+
let callToString () =
5+
// <Snippet2>
6+
let bytes = [| 0; 1; 14; 168; 255 |]
7+
for byteValue in bytes do
8+
printfn $"{byteValue}"
9+
10+
// The example displays the following output to the console if the current
11+
// culture is en-US:
12+
// 0
13+
// 1
14+
// 14
15+
// 168
16+
// 255
17+
// </Snippet2>
18+
19+
let specifyFormatProvider () =
20+
// <Snippet3>
21+
let bytes = [| 0; 1; 14; 168; 255 |]
22+
let providers =
23+
[ CultureInfo "en-us"
24+
CultureInfo "fr-fr"
25+
CultureInfo "de-de"
26+
CultureInfo "es-es" ]
27+
28+
for byteValue in bytes do
29+
for provider in providers do
30+
printf $"{byteValue.ToString provider,3} ({provider.Name}) "
31+
32+
printfn ""
33+
34+
// The example displays the following output to the console:
35+
// 0 (en-US) 0 (fr-FR) 0 (de-DE) 0 (es-ES)
36+
// 1 (en-US) 1 (fr-FR) 1 (de-DE) 1 (es-ES)
37+
// 14 (en-US) 14 (fr-FR) 14 (de-DE) 14 (es-ES)
38+
// 168 (en-US) 168 (fr-FR) 168 (de-DE) 168 (es-ES)
39+
// 255 (en-US) 255 (fr-FR) 255 (de-DE) 255 (es-ES)
40+
// </Snippet3>
41+
42+
let specifyFormatString () =
43+
// <Snippet4>
44+
let formats =
45+
[ "C3"; "D4"; "e1"; "E2"; "F1"; "G"; "N1"
46+
"P0"; "X4"; "0000.0000" ]
47+
let number = 240uy
48+
for format in formats do
49+
printfn $"'{format}' format specifier: {number.ToString format}"
50+
51+
// The example displays the following output to the console if the
52+
// current culture is en-us:
53+
// 'C3' format specifier: $240.000
54+
// 'D4' format specifier: 0240
55+
// 'e1' format specifier: 2.4e+002
56+
// 'E2' format specifier: 2.40E+002
57+
// 'F1' format specifier: 240.0
58+
// 'G' format specifier: 240
59+
// 'N1' format specifier: 240.0
60+
// 'P0' format specifier: 24,000 %
61+
// 'X4' format specifier: 00F0
62+
// '0000.0000' format specifier: 0240.0000
63+
// </Snippet4>
64+
65+
let formatWithProviders () =
66+
// <Snippet5>
67+
let byteValue = 250uy
68+
let providers =
69+
[ CultureInfo "en-us"
70+
CultureInfo "fr-fr"
71+
CultureInfo "es-es"
72+
CultureInfo "de-de" ]
73+
74+
for provider in providers do
75+
printfn $"""{byteValue.ToString("N2", provider)} ({provider.Name})"""
76+
77+
// The example displays the following output to the console:
78+
// 250.00 (en-US)
79+
// 250,00 (fr-FR)
80+
// 250,00 (es-ES)
81+
// 250,00 (de-DE)
82+
// </Snippet5>
83+
84+
callToString ()
85+
printfn ""
86+
specifyFormatProvider ()
87+
printfn ""
88+
specifyFormatString ()
89+
printfn ""
90+
formatWithProviders ()
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="NewByteMembers.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module TryParse
2+
3+
// <Snippet1>
4+
open System
5+
6+
let callTryParse (stringToConvert: string) =
7+
match Byte.TryParse stringToConvert with
8+
| true, byteValue ->
9+
printfn $"Converted '{stringToConvert}' to {byteValue}"
10+
| _ ->
11+
printfn $"Attempted conversion of '{stringToConvert}' failed."
12+
13+
let byteStrings =
14+
[ null; String.Empty; "1024"
15+
"100.1"; "100"; "+100"; "-100"
16+
"000000000000000100"; "00,100"
17+
" 20 "; "FF"; "0x1F" ]
18+
19+
for byteString in byteStrings do
20+
callTryParse byteString
21+
22+
// The example displays the following output to the console:
23+
// Attempted conversion of '' failed.
24+
// Attempted conversion of '' failed.
25+
// Attempted conversion of '1024' failed.
26+
// Attempted conversion of '100.1' failed.
27+
// Converted '100' to 100
28+
// Converted '+100' to 100
29+
// Attempted conversion of '-100' failed.
30+
// Converted '000000000000000100' to 100
31+
// Attempted conversion of '00,100' failed.
32+
// Converted ' 20 ' to 20
33+
// Attempted conversion of 'FF' failed.
34+
// Attempted conversion of '0x1F' failed.
35+
// </Snippet1>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module TryParse2
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
8+
match Byte.TryParse(stringToConvert, styles, null) with
9+
| true, byteValue ->
10+
printfn $"Converted '{stringToConvert}' to {byteValue}"
11+
| _ ->
12+
printfn $"Attempted conversion of '{stringToConvert}' failed."
13+
14+
[<EntryPoint>]
15+
let main _ =
16+
let byteString = "1024"
17+
let styles = NumberStyles.Integer
18+
callTryParse byteString styles
19+
20+
let byteString = "100.1"
21+
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
22+
callTryParse byteString styles
23+
24+
let byteString = "100.0"
25+
callTryParse byteString styles
26+
27+
let byteString = "+100"
28+
let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign
29+
callTryParse byteString styles
30+
31+
let byteString = "-100"
32+
callTryParse byteString styles
33+
34+
let byteString = "000000000000000100"
35+
callTryParse byteString styles
36+
37+
let byteString = "00,100"
38+
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
39+
callTryParse byteString styles
40+
41+
let byteString = "2E+3 "
42+
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
43+
callTryParse byteString styles
44+
45+
let byteString = "FF"
46+
let styles = NumberStyles.HexNumber
47+
callTryParse byteString styles
48+
49+
let byteString = "0x1F"
50+
callTryParse byteString styles
51+
52+
0
53+
54+
// The example displays the following output to the console:
55+
// Attempted conversion of '1024' failed.
56+
// Attempted conversion of '100.1' failed.
57+
// Converted '100.0' to 100
58+
// Converted '+100' to 100
59+
// Attempted conversion of '-100' failed.
60+
// Converted '000000000000000100' to 100
61+
// Converted '00,100' to 100
62+
// Attempted conversion of '2E+3 ' failed.
63+
// Converted 'FF' to 255
64+
// Attempted conversion of '0x1F' failed.
65+
// </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="TryParse.fs" />
8+
<Compile Include="TryParse2.fs" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)