diff --git a/snippets/fsharp/System/ParamArrayAttribute/Overview/Example.fs b/snippets/fsharp/System/ParamArrayAttribute/Overview/Example.fs new file mode 100644 index 00000000000..9c07db9b0d6 --- /dev/null +++ b/snippets/fsharp/System/ParamArrayAttribute/Overview/Example.fs @@ -0,0 +1,64 @@ +// +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([]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 _ -> () +// + +// +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/ParamArrayAttribute/Overview/fs.fsproj b/snippets/fsharp/System/ParamArrayAttribute/Overview/fs.fsproj new file mode 100644 index 00000000000..6ad09df043d --- /dev/null +++ b/snippets/fsharp/System/ParamArrayAttribute/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/xml/System/ParamArrayAttribute.xml b/xml/System/ParamArrayAttribute.xml index 3b573cb8d25..afa3c2bba5e 100644 --- a/xml/System/ParamArrayAttribute.xml +++ b/xml/System/ParamArrayAttribute.xml @@ -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"::: ]]>