Skip to content

Commit 7034f43

Browse files
albert-dudsyme
andauthored
System.Action<T1,T2,T3> F# Snippets (dotnet#7404)
* Add System.Action<T1,T2,T3> F# Snippets * Update samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~3/fs/Action3.fs Co-authored-by: Don Syme <[email protected]> * Update Action3.fs Co-authored-by: Don Syme <[email protected]>
1 parent 012dd08 commit 7034f43

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module TestAction3
2+
3+
// <Snippet2>
4+
open System
5+
6+
let copyStrings (source: string []) (target: string []) startPos =
7+
if source.Length <> target.Length then
8+
raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")
9+
10+
for i = startPos to source.Length - 1 do
11+
target.[i] <- source.[i]
12+
13+
let ordinals = [| "First"; "Second"; "Third"; "Fourth"; "Fifth" |]
14+
let copiedOrdinals = Array.zeroCreate<string> ordinals.Length
15+
16+
let copyOperation = Action<_,_,_> copyStrings
17+
18+
copyOperation.Invoke(ordinals, copiedOrdinals, 3)
19+
20+
for ordinal in copiedOrdinals do
21+
printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
22+
23+
// </Snippet2>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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="Action3.fs" />
10+
<Compile Include="Delegate.fs" />
11+
<Compile Include="Lambda.fs" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module TestDelegate
2+
3+
// <Snippet1>
4+
open System
5+
6+
type StringCopy = delegate of stringArray1: string [] *
7+
stringArray2: string [] *
8+
indexToStart: int -> unit
9+
10+
let copyStrings (source: string []) (target: string []) startPos =
11+
if source.Length <> target.Length then
12+
raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")
13+
14+
for i = startPos to source.Length - 1 do
15+
target.[i] <- source.[i]
16+
17+
let ordinals = [| "First"; "Second"; "Third"; "Fourth"; "Fifth" |]
18+
let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length
19+
20+
let copyOperation = StringCopy copyStrings
21+
22+
copyOperation.Invoke(ordinals, copiedOrdinals, 3)
23+
24+
for ordinal in copiedOrdinals do
25+
printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
26+
27+
// </Snippet1>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module TestLambda
2+
3+
// <Snippet4>
4+
open System
5+
6+
let copyStrings (source: string []) (target: string []) startPos =
7+
if source.Length <> target.Length then
8+
raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")
9+
10+
for i = startPos to source.Length - 1 do
11+
target.[i] <- source.[i]
12+
13+
let ordinals = [| "First"; "Second"; "Third"; "Fourth"; "Fifth" |]
14+
let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length
15+
16+
let copyOperation = Action<_,_,_> (fun s1 s2 pos -> copyStrings s1 s2 pos)
17+
18+
copyOperation.Invoke(ordinals, copiedOrdinals, 3)
19+
20+
for ordinal in copiedOrdinals do
21+
printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
22+
23+
// </Snippet4>

xml/System/Action`3.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,31 @@
9292
<format type="text/markdown"><![CDATA[
9393
9494
## Remarks
95-
You can use the <xref:System.Action%603> 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 three 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.
95+
You can use the <xref:System.Action%603> 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 three 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.
9696
9797
> [!NOTE]
9898
> To reference a method that has three parameters and returns a value, use the generic <xref:System.Func%604> delegate instead.
9999
100100
When you use the <xref:System.Action%603> delegate, you do not have to explicitly define a delegate that encapsulates a method with three parameters. For example, the following code explicitly declares a delegate named `StringCopy` and assigns a reference to the `CopyStrings` method to its delegate instance.
101101
102102
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~3/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1":::
103+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~3/fs/Delegate.fs" id="Snippet1":::
103104
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~3/vb/Delegate.vb" id="Snippet1":::
104105
105106
The following example simplifies this code by instantiating the <xref:System.Action%603> delegate instead of explicitly defining a new delegate and assigning a named method to it.
106107
107108
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~3/cs/Action3.cs" interactive="try-dotnet" id="Snippet2":::
109+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~3/fs/Action3.fs" id="Snippet2":::
108110
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~3/vb/Action3.vb" id="Snippet2":::
109111
110112
You can also use the <xref:System.Action%603> 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).)
111113
112114
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~3/cs/Anon.cs" interactive="try-dotnet" id="Snippet3":::
113115
114-
You can also assign a lambda expression to an <xref:System.Action%603> 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).)
116+
You can also assign a lambda expression to an <xref:System.Action%603> 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).)
115117
116118
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~3/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4":::
119+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~3/fs/Lambda.fs" id="Snippet4":::
117120
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~3/vb/lambda.vb" id="Snippet4":::
118121
119122
]]></format>

0 commit comments

Comments
 (0)