Skip to content

Commit 055d010

Browse files
authored
Add System.Action<T1,T2,T3,T4> F# Snippets (dotnet#7405)
1 parent 5e1d05a commit 055d010

File tree

5 files changed

+102
-2
lines changed

5 files changed

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

xml/System/Action`4.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,31 @@
100100
<format type="text/markdown"><![CDATA[
101101
102102
## Remarks
103-
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.
103+
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.
104104
105105
> [!NOTE]
106106
> To reference a method that has four parameters and returns a value, use the generic <xref:System.Func%605> delegate instead.
107107
108108
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.
109109
110110
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Delegate.cs" id="Snippet1":::
111+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Delegate.fs" id="Snippet1":::
111112
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~4/vb/Delegate.vb" id="Snippet1":::
112113
113114
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.
114115
115116
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Action4.cs" id="Snippet2":::
117+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Action4.fs" id="Snippet2":::
116118
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~4/vb/Action4.vb" id="Snippet2":::
117119
118120
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).)
119121
120122
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Anon.cs" id="Snippet3":::
121123
122-
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).)
124+
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).)
123125
124126
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~4/cs/Lambda.cs" id="Snippet4":::
127+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~4/fs/Lambda.fs" id="Snippet4":::
125128
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~4/vb/lambda.vb" id="Snippet4":::
126129
127130
]]></format>

0 commit comments

Comments
 (0)