Skip to content

Commit 012dd08

Browse files
albert-dudsyme
andauthored
System.Action<T1,T2> F# Snippets (dotnet#7403)
* Add System.Action<T1,T2> F# Snippets * update snippets Co-Authored-By: Don Syme <[email protected]> Co-authored-by: Don Syme <[email protected]>
1 parent 7510963 commit 012dd08

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module TestLambdaExpression
2+
3+
// <Snippet2>
4+
open System
5+
open System.IO
6+
7+
let message1 = "The first line of a message"
8+
let message2 = "The second line of a message"
9+
10+
let writeToConsole string1 string2 =
11+
printfn $"{string1}\n{string2}"
12+
13+
let writeToFile string1 string2 =
14+
use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false)
15+
writer.WriteLine $"{string1}\n{string2}"
16+
17+
let concat =
18+
Action<string, string>(fun string1 string2 ->
19+
if Environment.GetCommandLineArgs().Length > 1 then
20+
writeToFile string1 string2
21+
else
22+
writeToConsole string1 string2
23+
)
24+
25+
concat.Invoke(message1, message2)
26+
27+
// </Snippet2>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="Action2.fs" />
10+
<Compile Include="Delegate.fs" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module TestDelegate
2+
3+
// <Snippet1>
4+
open System
5+
open System.IO
6+
7+
type ConcatStrings = delegate of string1: string * string1: string -> unit
8+
9+
let message1 = "The first line of a message"
10+
let message2 = "The second line of a message"
11+
12+
let writeToConsole string1 string2 =
13+
printfn $"{string1}\n{string2}"
14+
15+
let writeToFile string1 string2 =
16+
use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false)
17+
writer.WriteLine $"{string1}\n{string2}"
18+
19+
let concat =
20+
ConcatStrings(fun string1 string2 ->
21+
if Environment.GetCommandLineArgs().Length > 1 then
22+
writeToFile string1 string2
23+
else
24+
writeToConsole string1 string2
25+
)
26+
27+
concat.Invoke(message1, message2)
28+
29+
// </Snippet1>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module TestDelegate
2+
3+
// <Snippet4>
4+
open System
5+
open System.IO
6+
7+
let message1 = "The first line of a message"
8+
let message2 = "The second line of a message"
9+
10+
let writeToConsole string1 string2 =
11+
printfn $"{string1}\n{string2}"
12+
13+
let writeToFile string1 string2 =
14+
use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false)
15+
writer.WriteLine $"{string1}\n{string2}"
16+
17+
let concat =
18+
Action<string,string>(
19+
if Environment.GetCommandLineArgs().Length > 1 then
20+
fun s1 s2 -> writeToFile s1 s2
21+
else
22+
fun s1 s2 -> writeToConsole s1 s2
23+
)
24+
25+
concat.Invoke(message1, message2)
26+
27+
// </Snippet4>

xml/System/Action`2.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,31 @@
8484
<format type="text/markdown"><![CDATA[
8585
8686
## Remarks
87-
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.
87+
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.
8888
8989
> [!NOTE]
9090
> To reference a method that has two parameters and returns a value, use the generic <xref:System.Func%603> delegate instead.
9191
9292
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.
9393
9494
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1":::
95+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs" id="Snippet1":::
9596
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/Delegate.vb" id="Snippet1":::
9697
9798
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.
9899
99100
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Action2.cs" interactive="try-dotnet" id="Snippet2":::
101+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs" id="Snippet2":::
100102
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/action2.vb" id="Snippet2":::
101103
102104
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).)
103105
104106
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Anon.cs" interactive="try-dotnet" id="Snippet3":::
105107
106-
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).)
108+
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).)
107109
108110
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4":::
111+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs" id="Snippet4":::
109112
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/lambda.vb" id="Snippet4":::
110113
111114
]]></format>

0 commit comments

Comments
 (0)