Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.ParamArrayAttribute F# snippets #7820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions snippets/fsharp/System/ParamArrayAttribute/Overview/Example.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// <Snippet1>
open System

type Temperature(temperature) =
override this.ToString() =
this.ToString "C"

member _.ToString(format) =
let format =
if String.IsNullOrEmpty format then "G"
else format

match format.ToUpper() with
| "G" | "C" ->
$"{temperature:N} °C"
| "F" ->
$"{9. * temperature / 5. + 32.:N} °F"
| "K" ->
$"{temperature + 273.15:N} °K"
| _ ->
raise (FormatException $"The '{format}' format specifier is not supported")

member this.Display([<ParamArray>]formats: string[]) =
if formats.Length = 0 then
printfn $"""{this.ToString "G"}"""
else
for format in formats do
try
printfn $"{this.ToString format}"
// If there is an exception, do nothing.
with _ -> ()
// </Snippet1>

// <Snippet2>
let temp1 = Temperature 100.
let formats = [| "C"; "G"; "F"; "K" |]

// Call Display method with a string array.
printfn "Calling Display with a string array:"
temp1.Display formats

// Call Display method with individual string arguments.
printfn "\nCalling Display with individual arguments:"
temp1.Display("C", "F", "K", "G")

// Call parameterless Display method.
printfn "\nCalling Display with an implicit parameter array:"
temp1.Display()
// The example displays the following output:
// Calling Display with a string array:
// 100.00 °C
// 100.00 °C
// 212.00 °F
// 373.15 °K
//
// Calling Display with individual arguments:
// 100.00 °C
// 212.00 °F
// 373.15 °K
// 100.00 °C
//
// Calling Display with an implicit parameter array:
// 100.00 °C
// </Snippet2>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/ParamArrayAttribute/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Example.fs" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions xml/System/ParamArrayAttribute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@
The following example defines a `Temperature` class that includes a `Display` method, which is intended to display one or more formatted temperature values. The method has a single parameter, `formats`, which is defined as a parameter array.

:::code language="csharp" source="~/snippets/csharp/System/ParamArrayAttribute/Overview/Example.cs" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/ParamArrayAttribute/Overview/Example.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.ParamArrayAttribute/vb/Example.vb" id="Snippet1":::

The following example illustrates three different calls to the `Temperature.Display` method. In the first, the method is passed an array of format strings. In the second, the method is passed four individual format strings as arguments. In the third, the method is called with no arguments. As the output from the example illustrates, the Visual Basic and C# compilers translate this into a call to the `Display` method with an empty string array.

:::code language="csharp" source="~/snippets/csharp/System/ParamArrayAttribute/Overview/Example.cs" id="Snippet2":::
:::code language="fsharp" source="~/snippets/fsharp/System/ParamArrayAttribute/Overview/Example.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.ParamArrayAttribute/vb/Example.vb" id="Snippet2":::

]]></format>
Expand Down