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

Skip to content

System.Action<T1,T2,T3,T4> F# Snippets #7405

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 1 commit into from
Dec 16, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module TestAction4

// <Snippet2>
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 "<None>" else ordinal)

// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Action4.fs" />
<Compile Include="Delegate.fs" />
<Compile Include="Lambda.fs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module TestDelegate

// <Snippet1>
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 "<None>" else ordinal)

// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module TestLambdaExpression

// <Snippet4>
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 "<None>" else ordinal)

// </Snippet4>
7 changes: 5 additions & 2 deletions xml/System/Action`4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,31 @@
<format type="text/markdown"><![CDATA[

## Remarks
You can use the <xref:System.Action%604> 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 <xref:System.Action%604> 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 <xref:System.Func%605> delegate instead.

When you use the <xref:System.Action%604> 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 <xref:System.Action%604> 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 <xref:System.Action%604> 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 <xref:System.Action%604> 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 <xref:System.Action%604> 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":::

]]></format>
Expand Down