From 2079f5197b1f0990bc94add629de16e5bd412b44 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Thu, 4 Nov 2021 19:55:56 -0700 Subject: [PATCH] Add System.Action F# Snippets --- .../system.Action~4/fs/Action4.fs | 26 ++++++++++++++++ .../system.Action~4/fs/Action4.fsproj | 14 +++++++++ .../system.Action~4/fs/Delegate.fs | 31 +++++++++++++++++++ .../system.Action~4/fs/Lambda.fs | 26 ++++++++++++++++ xml/System/Action`4.xml | 7 +++-- 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fsproj create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Delegate.fs create mode 100644 samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Lambda.fs diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fs new file mode 100644 index 00000000000..e5a11fe51d3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fs @@ -0,0 +1,26 @@ +module TestAction4 + +// +open System + +let copyStrings (source: string []) (target: string []) startPos number = + if source.Length <> target.Length then + raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.") + + for i = startPos to startPos + number - 1 do + target.[i] <- source.[i] + +let ordinals = + [| "First"; "Second"; "Third"; "Fourth"; "Fifth" + "Sixth"; "Seventh"; "Eighth"; "Ninth"; "Tenth" |] + +let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length + +let copyOperation = Action<_, _, _, _> copyStrings + +copyOperation.Invoke(ordinals, copiedOrdinals, 3, 5) + +for ordinal in copiedOrdinals do + printfn "%s" (if String.IsNullOrEmpty ordinal then "" else ordinal) + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fsproj new file mode 100644 index 00000000000..818dce26e5f --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fsproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Delegate.fs new file mode 100644 index 00000000000..466a1b7b2f7 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Delegate.fs @@ -0,0 +1,31 @@ +module TestDelegate + +// +open System + +type StringCopy = delegate of stringArray1: string [] * + stringArray2: string [] * + indexToStart: int * + numberToCopy: int -> unit + +let copyStrings (source: string []) (target: string []) startPos number = + if source.Length <> target.Length then + raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.") + + for i = startPos to startPos + number - 1 do + target.[i] <- source.[i] + +let ordinals = + [| "First"; "Second"; "Third"; "Fourth"; "Fifth" + "Sixth"; "Seventh"; "Eighth"; "Ninth"; "Tenth" |] + +let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length + +let copyOperation = StringCopy copyStrings + +copyOperation.Invoke(ordinals, copiedOrdinals, 3, 5) + +for ordinal in copiedOrdinals do + printfn "%s" (if String.IsNullOrEmpty ordinal then "" else ordinal) + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Lambda.fs new file mode 100644 index 00000000000..b0510184eb0 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Lambda.fs @@ -0,0 +1,26 @@ +module TestLambdaExpression + +// +open System + +let copyStrings (source: string []) (target: string []) startPos number = + if source.Length <> target.Length then + raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.") + + for i = startPos to startPos + number - 1 do + target.[i] <- source.[i] + +let ordinals = + [| "First"; "Second"; "Third"; "Fourth"; "Fifth" + "Sixth"; "Seventh"; "Eighth"; "Ninth"; "Tenth" |] + +let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length + +let copyOperation = Action<_, _, _, _> (fun s1 s2 pos num -> copyStrings s1 s2 pos num) + +copyOperation.Invoke(ordinals, copiedOrdinals, 3, 5) + +for ordinal in copiedOrdinals do + printfn "%s" (if String.IsNullOrEmpty ordinal then "" else ordinal) + +// \ No newline at end of file diff --git a/xml/System/Action`4.xml b/xml/System/Action`4.xml index 6b19748edec..c851515112d 100644 --- a/xml/System/Action`4.xml +++ b/xml/System/Action`4.xml @@ -100,7 +100,7 @@ delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have four parameters that are all passed to it by value, and it must not return a value. (In C#, the method must return `void`. In Visual Basic, it must be defined by the `Sub`…`End Sub` construct. It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation. + You can use the delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have four parameters that are all passed to it by value, and it must not return a value. (In C#, the method must return `void`. In F#, the method or function must return unit. In Visual Basic, it must be defined by the `Sub`…`End Sub` construct. It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation. > [!NOTE] > To reference a method that has four parameters and returns a value, use the generic delegate instead. @@ -108,20 +108,23 @@ When you use the delegate, you do not have to explicitly define a delegate that encapsulates a method with four parameters. For example, the following code explicitly declares a delegate named `StringCopy` and assigns a reference to the `CopyStrings` method to its delegate instance. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Delegate.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~4/vb/Delegate.vb" id="Snippet1"::: The following example simplifies this code by instantiating the delegate instead of explicitly defining a new delegate and assigning a named method to it. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Action4.cs" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~4/vb/Action4.vb" id="Snippet2"::: You can also use the delegate with anonymous methods in C#, as the following example illustrates. (For an introduction to anonymous methods, see [Anonymous Methods](/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-methods).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Anon.cs" id="Snippet3"::: - You can also assign a lambda expression to an delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).) + You can also assign a lambda expression to an delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions (C#)](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) or [Lambda Expressions (F#)](/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Lambda.cs" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~4/vb/lambda.vb" id="Snippet4"::: ]]>