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

Skip to content

System.Action<T1,T2> F# Snippets #7403

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 2 commits into from
Nov 23, 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,27 @@
module TestLambdaExpression

// <Snippet2>
open System
open System.IO

let message1 = "The first line of a message"
let message2 = "The second line of a message"

let writeToConsole string1 string2 =
printfn $"{string1}\n{string2}"

let writeToFile string1 string2 =
use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false)
writer.WriteLine $"{string1}\n{string2}"

let concat =
Action<string, string>(fun string1 string2 ->
if Environment.GetCommandLineArgs().Length > 1 then
writeToFile string1 string2
else
writeToConsole string1 string2
)

concat.Invoke(message1, message2)

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

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

<ItemGroup>
<Compile Include="Action2.fs" />
<Compile Include="Delegate.fs" />
</ItemGroup>

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

// <Snippet1>
open System
open System.IO

type ConcatStrings = delegate of string1: string * string1: string -> unit

let message1 = "The first line of a message"
let message2 = "The second line of a message"

let writeToConsole string1 string2 =
printfn $"{string1}\n{string2}"

let writeToFile string1 string2 =
use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false)
writer.WriteLine $"{string1}\n{string2}"

let concat =
ConcatStrings(fun string1 string2 ->
if Environment.GetCommandLineArgs().Length > 1 then
writeToFile string1 string2
else
writeToConsole string1 string2
)

concat.Invoke(message1, message2)

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

// <Snippet4>
open System
open System.IO

let message1 = "The first line of a message"
let message2 = "The second line of a message"

let writeToConsole string1 string2 =
printfn $"{string1}\n{string2}"

let writeToFile string1 string2 =
use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false)
writer.WriteLine $"{string1}\n{string2}"

let concat =
Action<string,string>(
if Environment.GetCommandLineArgs().Length > 1 then
fun s1 s2 -> writeToFile s1 s2
else
fun s1 s2 -> writeToConsole s1 s2
)

concat.Invoke(message1, message2)

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

## Remarks
You can use the <xref:System.Action%602> 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 two parameters that are both 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%602> 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 two parameters that are both 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 two parameters and returns a value, use the generic <xref:System.Func%603> delegate instead.

When you use the <xref:System.Action%602> delegate, you do not have to explicitly define a delegate that encapsulates a method with two parameters. For example, the following code explicitly declares a delegate named `ConcatStrings`. It then assigns a reference to either of two methods to its delegate instance. One method writes two strings to the console; the second writes two strings to a file.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/Delegate.vb" id="Snippet1":::

The following example simplifies this code by instantiating the <xref:System.Action%602> 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~2/cs/Action2.cs" interactive="try-dotnet" id="Snippet2":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs" id="Snippet2":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/action2.vb" id="Snippet2":::

You can also use the <xref:System.Action%602> 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~2/cs/Anon.cs" interactive="try-dotnet" id="Snippet3":::

You can also assign a lambda expression to an <xref:System.Action%602> 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%602> 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~2/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs" id="Snippet4":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/lambda.vb" id="Snippet4":::

]]></format>
Expand Down