diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/fs.fsproj new file mode 100644 index 00000000000..bce9bb0f36b --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/systembyte.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/systembyte.fs new file mode 100644 index 00000000000..fd502e0dea7 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/systembyte.fs @@ -0,0 +1,27 @@ +open System + +// +let minMaxFields numberToSet = + if numberToSet <= int Byte.MaxValue && numberToSet >= int Byte.MinValue then + // You must explicitly convert an integer to a byte. + let myByte = byte numberToSet + + printfn $"The MemberByte value is {myByte}" + else + printfn $"The value {numberToSet} is outside of the range of possible Byte values" + +// + +// +let compare (byte1: byte) byte2 = + let myCompareResult = byte1.CompareTo byte2 + + if myCompareResult > 0 then + printfn $"{byte1} is less than the MemberByte value {byte2}" + + elif myCompareResult < 0 then + printfn $"{byte1} is greater than the MemberByte value {byte2}" + + else + printfn $"{byte1} is equal to the MemberByte value {byte2}" +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/fs.fsproj new file mode 100644 index 00000000000..7aecbf41458 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/parse.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/parse.fs new file mode 100644 index 00000000000..21853e6aba7 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/parse.fs @@ -0,0 +1,120 @@ +open System +open System.Globalization + +let callParse1 () = + // + let stringToConvert = " 162" + try + let byteValue = Byte.Parse stringToConvert + printfn $"Converted '{stringToConvert}' to {byteValue}." + with + | :? FormatException -> + printfn $"Unable to parse '{stringToConvert}'." + | :? OverflowException -> + printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." + + // The example displays the following output to the console: + // Converted ' 162' to 162. + // + +let callParse2 () = + // + let stringToConvert = " 214 " + try + let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture) + printfn $"Converted '{stringToConvert}' to {byteValue}." + with + | :? FormatException -> + printfn $"Unable to parse '{stringToConvert}'." + | :? OverflowException -> + printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." + + let stringToConvert = " + 214 " + try + let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture) + printfn $"Converted '{stringToConvert}' to {byteValue}." + with + | :? FormatException -> + printfn $"Unable to parse '{stringToConvert}'." + | :? OverflowException -> + printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." + + let stringToConvert = " +214 " + try + let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture) + printfn $"Converted '{stringToConvert}' to {byteValue}." + with + | :? FormatException -> + printfn $"Unable to parse '{stringToConvert}'." + | :? OverflowException -> + printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." + + // The example displays the following output to the console: + // Converted ' 214 ' to 214. + // Unable to parse ' + 214 '. + // Converted ' +214 ' to 214. + // + +let callParse3 () = + // + // Parse value with no styles allowed. + let style = NumberStyles.None + let value = " 241 " + try + let number = Byte.Parse(value, style); + printfn $"Converted '{value}' to {number}." + with :? FormatException -> + printfn $"Unable to parse '{value}'." + + // Parse value with trailing sign. + let style = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign + let value = " 163+" + let number = Byte.Parse(value, style) + printfn $"Converted '{value}' to {number}." + + // Parse value with leading sign. + let value = " +253 " + let number = Byte.Parse(value, style) + printfn $"Converted '{value}' to {number}." + + // This example displays the following output to the console: + // Unable to parse ' 241 '. + // Converted ' 163+' to 163. + // Converted ' +253 ' to 253. + // + +let callParse4 () = + // + // Parse number with decimals. + // NumberStyles.Float includes NumberStyles.AllowDecimalPoint. + let style = NumberStyles.Float + let culture = CultureInfo.CreateSpecificCulture "fr-FR" + let value = "12,000" + + let number = Byte.Parse(value, style, culture) + printfn $"Converted '{value}' to {number}." + + let culture = CultureInfo.CreateSpecificCulture "en-GB" + try + let number = Byte.Parse(value, style, culture) + printfn $"Converted '{value}' to {number}." + with :? FormatException -> + printfn $"Unable to parse '{value}'." + + let value = "12.000" + let number = Byte.Parse(value, style, culture) + printfn $"Converted '{value}' to {number}." + + // The example displays the following output to the console: + // Converted '12,000' to 12. + // Unable to parse '12,000'. + // Converted '12.000' to 12. + // + +callParse1 () +printfn "" +callParse2 () +printfn "" +callParse3 () +printfn "" +callParse4 () \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/NewByteMembers.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/NewByteMembers.fs new file mode 100644 index 00000000000..e69e5fed6c4 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/NewByteMembers.fs @@ -0,0 +1,90 @@ +open System +open System.Globalization + +let callToString () = + // + let bytes = [| 0; 1; 14; 168; 255 |] + for byteValue in bytes do + printfn $"{byteValue}" + + // The example displays the following output to the console if the current + // culture is en-US: + // 0 + // 1 + // 14 + // 168 + // 255 + // + +let specifyFormatProvider () = + // + let bytes = [| 0; 1; 14; 168; 255 |] + let providers = + [ CultureInfo "en-us" + CultureInfo "fr-fr" + CultureInfo "de-de" + CultureInfo "es-es" ] + + for byteValue in bytes do + for provider in providers do + printf $"{byteValue.ToString provider,3} ({provider.Name}) " + + printfn "" + + // The example displays the following output to the console: + // 0 (en-US) 0 (fr-FR) 0 (de-DE) 0 (es-ES) + // 1 (en-US) 1 (fr-FR) 1 (de-DE) 1 (es-ES) + // 14 (en-US) 14 (fr-FR) 14 (de-DE) 14 (es-ES) + // 168 (en-US) 168 (fr-FR) 168 (de-DE) 168 (es-ES) + // 255 (en-US) 255 (fr-FR) 255 (de-DE) 255 (es-ES) + // + +let specifyFormatString () = + // + let formats = + [ "C3"; "D4"; "e1"; "E2"; "F1"; "G"; "N1" + "P0"; "X4"; "0000.0000" ] + let number = 240uy + for format in formats do + printfn $"'{format}' format specifier: {number.ToString format}" + + // The example displays the following output to the console if the + // current culture is en-us: + // 'C3' format specifier: $240.000 + // 'D4' format specifier: 0240 + // 'e1' format specifier: 2.4e+002 + // 'E2' format specifier: 2.40E+002 + // 'F1' format specifier: 240.0 + // 'G' format specifier: 240 + // 'N1' format specifier: 240.0 + // 'P0' format specifier: 24,000 % + // 'X4' format specifier: 00F0 + // '0000.0000' format specifier: 0240.0000 + // + +let formatWithProviders () = + // + let byteValue = 250uy + let providers = + [ CultureInfo "en-us" + CultureInfo "fr-fr" + CultureInfo "es-es" + CultureInfo "de-de" ] + + for provider in providers do + printfn $"""{byteValue.ToString("N2", provider)} ({provider.Name})""" + + // The example displays the following output to the console: + // 250.00 (en-US) + // 250,00 (fr-FR) + // 250,00 (es-ES) + // 250,00 (de-DE) + // + +callToString () +printfn "" +specifyFormatProvider () +printfn "" +specifyFormatString () +printfn "" +formatWithProviders () \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/fs.fsproj new file mode 100644 index 00000000000..109498b9ce8 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse.fs new file mode 100644 index 00000000000..077849ef3f3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse.fs @@ -0,0 +1,35 @@ +module TryParse + +// +open System + +let callTryParse (stringToConvert: string) = + match Byte.TryParse stringToConvert with + | true, byteValue -> + printfn $"Converted '{stringToConvert}' to {byteValue}" + | _ -> + printfn $"Attempted conversion of '{stringToConvert}' failed." + +let byteStrings = + [ null; String.Empty; "1024" + "100.1"; "100"; "+100"; "-100" + "000000000000000100"; "00,100" + " 20 "; "FF"; "0x1F" ] + +for byteString in byteStrings do + callTryParse byteString + +// The example displays the following output to the console: +// Attempted conversion of '' failed. +// Attempted conversion of '' failed. +// Attempted conversion of '1024' failed. +// Attempted conversion of '100.1' failed. +// Converted '100' to 100 +// Converted '+100' to 100 +// Attempted conversion of '-100' failed. +// Converted '000000000000000100' to 100 +// Attempted conversion of '00,100' failed. +// Converted ' 20 ' to 20 +// Attempted conversion of 'FF' failed. +// Attempted conversion of '0x1F' failed. +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse2.fs new file mode 100644 index 00000000000..b8876200078 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse2.fs @@ -0,0 +1,65 @@ +module TryParse2 + +// +open System +open System.Globalization + +let callTryParse (stringToConvert: string) (styles: NumberStyles) = + match Byte.TryParse(stringToConvert, styles, null) with + | true, byteValue -> + printfn $"Converted '{stringToConvert}' to {byteValue}" + | _ -> + printfn $"Attempted conversion of '{stringToConvert}' failed." + +[] +let main _ = + let byteString = "1024" + let styles = NumberStyles.Integer + callTryParse byteString styles + + let byteString = "100.1" + let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint + callTryParse byteString styles + + let byteString = "100.0" + callTryParse byteString styles + + let byteString = "+100" + let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign + callTryParse byteString styles + + let byteString = "-100" + callTryParse byteString styles + + let byteString = "000000000000000100" + callTryParse byteString styles + + let byteString = "00,100" + let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands + callTryParse byteString styles + + let byteString = "2E+3 " + let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent + callTryParse byteString styles + + let byteString = "FF" + let styles = NumberStyles.HexNumber + callTryParse byteString styles + + let byteString = "0x1F" + callTryParse byteString styles + + 0 + +// The example displays the following output to the console: +// Attempted conversion of '1024' failed. +// Attempted conversion of '100.1' failed. +// Converted '100.0' to 100 +// Converted '+100' to 100 +// Attempted conversion of '-100' failed. +// Converted '000000000000000100' to 100 +// Converted '00,100' to 100 +// Attempted conversion of '2E+3 ' failed. +// Converted 'FF' to 255 +// Attempted conversion of '0x1F' failed. +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/fs.fsproj new file mode 100644 index 00000000000..6101f0240e3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise1.fs new file mode 100644 index 00000000000..5226be1e856 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise1.fs @@ -0,0 +1,22 @@ +module bitwise1 + +// +open System +open System.Globalization + +let values = + [ Convert.ToString(12, 16) + Convert.ToString(123, 16) + Convert.ToString(245, 16) ] + +let mask = 0xFEuy +for value in values do + let byteValue = Byte.Parse(value, NumberStyles.AllowHexSpecifier) + printfn $"{byteValue} And {mask} = {byteValue &&& mask}" + + +// The example displays the following output: +// 12 And 254 = 12 +// 123 And 254 = 122 +// 245 And 254 = 244 +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise2.fs new file mode 100644 index 00000000000..210dbdbb06b --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise2.fs @@ -0,0 +1,33 @@ +module bitwise2 + +// +open System +open System.Collections.Generic +open System.Globalization + +[] +type ByteString = + { Sign: int + Value: string } + +let createArray values = + [ for value in values do + let sign = sign value + { Sign = sign + // Change two's complement to magnitude-only representation. + Value = Convert.ToString(value * sign, 16)} ] + + +let values = createArray [ -15; 123; 245 ] + +let mask = 0x14uy // Mask all bits but 2 and 4. + +for strValue in values do + let byteValue = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier) + printfn $"{strValue.Sign * int byteValue} ({Convert.ToString(byteValue, 2)}) And {mask} ({Convert.ToString(mask, 2)}) = {(strValue.Sign &&& (int mask |> sign)) * int (byteValue &&& mask)} ({Convert.ToString(byteValue &&& mask, 2)})" + +// The example displays the following output: +// -15 (1111) And 20 (10100) = 4 (100) +// 123 (1111011) And 20 (10100) = 16 (10000) +// 245 (11110101) And 20 (10100) = 20 (10100) +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/fs.fsproj new file mode 100644 index 00000000000..acb42bdf3a6 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.equals/fs/eq.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.equals/fs/eq.fs new file mode 100644 index 00000000000..2672479b4f0 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.equals/fs/eq.fs @@ -0,0 +1,16 @@ +// +let byteVal1 = 0x7fuy +let byteVal2 = 127uy +let objectVal3: obj = byteVal2 + +printfn $"byteVal1 = {byteVal1}, byteVal2 = {byteVal2}, objectVal3 = {objectVal3}\n" +printfn $"byteVal1 equals byteVal2?: {byteVal1.Equals byteVal2}" +printfn $"byteVal1 equals objectVal3?: {byteVal1.Equals objectVal3}" + +// This code example produces the following results: +// +// byteVal1 = 127, byteVal2 = 127, objectVal3 = 127 +// +// byteVal1 equals byteVal2?: True +// byteVal1 equals objectVal3?: True +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.equals/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.equals/fs/fs.fsproj new file mode 100644 index 00000000000..6ca25d319c7 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.equals/fs/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/formatting1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/formatting1.fs new file mode 100644 index 00000000000..270bf0e8291 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/formatting1.fs @@ -0,0 +1,47 @@ +open System + +let callToString () = + // + let numbers = [| 0; 16; 104; 213 |] + for number in numbers do + // Display value using default formatting. + number.ToString() + |> printf "%-3s --> " + + // Display value with 3 digits and leading zeros. + number.ToString "D3" + |> printf "%s " + + // Display value with hexadecimal. + number.ToString "X2" + |> printf "%s " + + // Display value with four hexadecimal digits. + number.ToString "X4" + |> printfn "%s" + + // The example displays the following output: + // 0 --> 000 00 0000 + // 16 --> 016 10 0010 + // 104 --> 104 68 0068 + // 213 --> 213 D5 00D5 + // + +let callConvert () = + // + let numbers = [| 0; 16; 104; 213 |] + printfn "%s %8s %5s %5s" "Value" "Binary" "Octal" "Hex" + for number in numbers do + printfn $"%5i{number} %8s{Convert.ToString(number, 2)} %5s{Convert.ToString(number, 8)} %5s{Convert.ToString(number, 16)}" + + // The example displays the following output: + // Value Binary Octal Hex + // 0 0 0 0 + // 16 10000 20 10 + // 104 1101000 150 68 + // 213 11010101 325 d5 + // + +callToString () +printfn "-----" +callConvert () \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/fs.fsproj new file mode 100644 index 00000000000..19c8c79ae0b --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/byteinstantiation1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/byteinstantiation1.fs new file mode 100644 index 00000000000..73d3855f0ae --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/byteinstantiation1.fs @@ -0,0 +1,56 @@ +open System + +let ``instantiate by assignment`` = + // + let value1 = 64uy + let value2 = 255uy + // + () + +let ``instantiateByNarrowingConversion`` = + // + let int1 = 128 + try + let value1 = byte int1 + printfn $"{value1}" + with :? OverflowException -> + printfn $"{int1} is out of range of a byte." + + let dbl2 = 3.997 + try + let value2 = byte dbl2 + printfn $"{value2}" + with :? OverflowException -> + printfn $"{dbl2} is out of range of a byte." + + // The example displays the following output: + // 128 + // 3 + // + +let parse = + // + let string1 = "244" + try + let byte1 = Byte.Parse string1 + printfn $"{byte1}" + with + | :? OverflowException -> + printfn $"'{string1}' is out of range of a byte." + | :? FormatException -> + printfn $"'{string1}' is out of range of a byte." + + let string2 = "F9" + try + let byte2 = Byte.Parse(string2, System.Globalization.NumberStyles.HexNumber) + printfn $"{byte2}" + with + | :? OverflowException -> + printfn $"'{string2}' is out of range of a byte." + | :? FormatException -> + printfn $"'{string2}' is out of range of a byte." + + // The example displays the following output: + // 244 + // 249 + // \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/fs.fsproj new file mode 100644 index 00000000000..35c00bf710c --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.convert.tobyte/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.convert.tobyte/fs/fs.fsproj new file mode 100644 index 00000000000..47d9503cf3c --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.convert.tobyte/fs/fs.fsproj @@ -0,0 +1,9 @@ + + + Exe + net6.0 + + + + + \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.convert.tobyte/fs/tobyte1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.convert.tobyte/fs/tobyte1.fs new file mode 100644 index 00000000000..3c2e03b91c1 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.convert.tobyte/fs/tobyte1.fs @@ -0,0 +1,22 @@ +// +open System + +let numbers = [| Int32.MinValue; -1; 0; 121; 340; Int32.MaxValue |] + +for number in numbers do + try + let result = Convert.ToByte number + printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}." + + with :? OverflowException -> + printfn $"The {number.GetType().Name} value {number} is outside the range of the Byte type." + + +// The example displays the following output: +// The Int32 value -2147483648 is outside the range of the Byte type. +// The Int32 value -1 is outside the range of the Byte type. +// Converted the Int32 value 0 to the Byte value 0. +// Converted the Int32 value 121 to the Byte value 121. +// The Int32 value 340 is outside the range of the Byte type. +// The Int32 value 2147483647 is outside the range of the Byte type. +// diff --git a/xml/System/Byte.xml b/xml/System/Byte.xml index ac0f44d02ae..5328ad5b5b1 100644 --- a/xml/System/Byte.xml +++ b/xml/System/Byte.xml @@ -101,21 +101,25 @@ - You can declare a variable and assign it a literal integer value that is within the range of the data type. The following example declares two variables and assigns them values in this way. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.instantiation/cs/byteinstantiation1.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/byteinstantiation1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.instantiation/vb/byteinstantiate1.vb" id="Snippet1"::: -- You can assign a non-byte numeric value to a byte. This is a narrowing conversion, so it requires a cast operator in C# and a conversion method in Visual Basic if `Option Strict` is on. If the non-byte value is a , , or value that includes a fractional component, the handling of its fractional part depends on the compiler performing the conversion. The following example assigns several numeric values to variables. +- You can assign a non-byte numeric value to a byte. This is a narrowing conversion, so it requires a cast operator in C# and F#, or a conversion method in Visual Basic if `Option Strict` is on. If the non-byte value is a , , or value that includes a fractional component, the handling of its fractional part depends on the compiler performing the conversion. The following example assigns several numeric values to variables. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.instantiation/cs/byteinstantiation1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/byteinstantiation1.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.instantiation/vb/byteinstantiate1.vb" id="Snippet2"::: - You can call a method of the class to convert any supported type to a value. This is possible because supports the interface. The following example illustrates the conversion of an array of values to values. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.convert.tobyte/cs/tobyte1.cs" interactive="try-dotnet-method" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.convert.tobyte/fs/tobyte1.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.convert.tobyte/vb/tobyte1.vb" id="Snippet4"::: - You can call the or method to convert the string representation of a value to a . The string can contain either decimal or hexadecimal digits. The following example illustrates the parse operation by using both a decimal and a hexadecimal string. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.instantiation/cs/byteinstantiation1.cs" interactive="try-dotnet-method" id="Snippet3"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.instantiation/fs/byteinstantiation1.fs" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.instantiation/vb/byteinstantiate1.vb" id="Snippet3"::: ## Performing Operations on Byte Values @@ -131,11 +135,13 @@ To format a value as an integral string with no leading zeros, you can call the parameterless method. By using the "D" format specifier, you can also include a specified number of leading zeros in the string representation. By using the "X" format specifier, you can represent a value as a hexadecimal string. The following example formats the elements in an array of values in these three ways. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.formatting/cs/formatting1.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/formatting1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.formatting/vb/formatting1.vb" id="Snippet1"::: You can also format a value as a binary, octal, decimal, or hexadecimal string by calling the method and supplying the base as the method's second parameter. The following example calls this method to display the binary, octal, and hexadecimal representations of an array of byte values. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.formatting/cs/formatting1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.formatting/fs/formatting1.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.formatting/vb/formatting1.vb" id="Snippet2"::: ## Working with Non-Decimal Byte Values @@ -146,11 +152,13 @@ When an operation is performed on two values, the values share the same representation, so the result is accurate. This is illustrated in the following example, which masks the lowest-order bit of a value to ensure that it is even. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.bitwise/cs/bitwise1.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise1.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.bitwise/vb/bitwise1.vb" id="Snippet1"::: On the other hand, when you work with both unsigned and signed bits, bitwise operations are complicated by the fact that the values use sign-and-magnitude representation for positive values, and two's complement representation for negative values. In order to perform a meaningful bitwise operation, the values must be converted to two equivalent representations, and information about the sign bit must be preserved. The following example does this to mask out bits 2 and 4 of an array of 8-bit signed and unsigned values. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.bitwise/cs/bitwise2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.bitwise/fs/bitwise2.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.bitwise/vb/bitwise2.vb" id="Snippet2"::: ]]> @@ -341,6 +349,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte Examples/CPP/systembyte.cpp" id="Snippet3"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte Examples/CS/systembyte.cs" id="Snippet3"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/systembyte.fs" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte Examples/VB/systembyte.vb" id="Snippet3"::: ]]> @@ -371,6 +380,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.byte.equals/cpp/eq.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.byte.equals/cs/eq.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.byte.equals/fs/eq.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.byte.equals/vb/eq.vb" id="Snippet1"::: ]]> @@ -620,6 +630,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte Examples/CPP/systembyte.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte Examples/CS/systembyte.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/systembyte.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte Examples/VB/systembyte.vb" id="Snippet1"::: ]]> @@ -678,6 +689,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte Examples/CPP/systembyte.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte Examples/CS/systembyte.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte Examples/FS/systembyte.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte Examples/VB/systembyte.vb" id="Snippet1"::: ]]> @@ -768,6 +780,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.Parse/cpp/parse.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.Parse/CS/parse.cs" interactive="try-dotnet-method" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/parse.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.Parse/VB/parse.vb" id="Snippet1"::: ]]> @@ -885,6 +898,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.Parse/cpp/parse.cpp" id="Snippet3"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.Parse/CS/parse.cs" interactive="try-dotnet-method" id="Snippet3"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/parse.fs" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.Parse/VB/parse.vb" id="Snippet3"::: ]]> @@ -987,6 +1001,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.Parse/cpp/parse.cpp" id="Snippet2"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.Parse/CS/parse.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/parse.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.Parse/VB/parse.vb" id="Snippet2"::: ]]> @@ -1147,6 +1162,7 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.Parse/cpp/parse.cpp" id="Snippet4"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.Parse/CS/parse.cs" interactive="try-dotnet-method" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.Parse/FS/parse.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.Parse/VB/parse.vb" id="Snippet4"::: ]]> @@ -2163,10 +2179,11 @@ This member is an explicit interface member implementation. It can be used only ## Examples - The following example displays an array of byte values. Note that the method is not called explicitly in the example. Instead, it is called implicitly, because of the use of the [composite formatting](/dotnet/standard/base-types/composite-formatting) feature. + The following example displays an array of byte values. Note that the method is not called explicitly in the example. Instead, it is called implicitly, because of the use of the [composite formatting](/dotnet/standard/base-types/composite-formatting) feature, the F# example uses [string interpolation](/dotnet/fsharp/language-reference/interpolated-strings). :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.ToString/CPP/newbytemembers2.cpp" id="Snippet2"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.ToString/CS/NewByteMembers.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/NewByteMembers.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.ToString/VB/NewByteMembers.vb" id="Snippet2"::: ]]> @@ -2256,6 +2273,7 @@ This member is an explicit interface member implementation. It can be used only :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.ToString/CPP/newbytemembers.cpp" id="Snippet3"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.ToString/CS/NewByteMembers.cs" interactive="try-dotnet-method" id="Snippet3"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/NewByteMembers.fs" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.ToString/VB/NewByteMembers.vb" id="Snippet3"::: ]]> @@ -2336,6 +2354,7 @@ This member is an explicit interface member implementation. It can be used only :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.ToString/CPP/newbytemembers2.cpp" id="Snippet4"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.ToString/CS/NewByteMembers.cs" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/NewByteMembers.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.ToString/VB/NewByteMembers.vb" id="Snippet4"::: ]]> @@ -2430,6 +2449,7 @@ This member is an explicit interface member implementation. It can be used only :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.ToString/CPP/newbytemembers2.cpp" id="Snippet5"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.ToString/CS/NewByteMembers.cs" interactive="try-dotnet-method" id="Snippet5"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.ToString/FS/NewByteMembers.fs" id="Snippet5"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.ToString/VB/NewByteMembers.vb" id="Snippet5"::: ]]> @@ -2628,6 +2648,7 @@ This member is an explicit interface member implementation. It can be used only :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.TryParse/cpp/tryparse.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.TryParse/cs/TryParse.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.TryParse/vb/TryParse.vb" id="Snippet1"::: ]]> @@ -2786,6 +2807,7 @@ This member is an explicit interface member implementation. It can be used only :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Byte.TryParse/cpp/tryparse2.cpp" id="Snippet2"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Byte.TryParse/cs/TryParse2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Byte.TryParse/fs/TryParse2.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Byte.TryParse/vb/TryParse2.vb" id="Snippet2"::: ]]>