Skip to content

Commit 16275f5

Browse files
authored
Add System.Action<T> F# snippets (#7380)
* Add System.Action<T> F# snippets * Update Lambda.fs * Update action.fs
1 parent aadc1be commit 16275f5

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
open System
2+
3+
//<snippet01>
4+
// F# provides a type alias for System.Collections.List<'T> as ResizeArray<'T>.
5+
let names = ResizeArray<string>()
6+
names.Add "Bruce"
7+
names.Add "Alfred"
8+
names.Add "Tim"
9+
names.Add "Richard"
10+
11+
let print s = printfn "%s" s
12+
13+
// Display the contents of the list using the print function.
14+
names.ForEach(Action<string> print)
15+
16+
// The following demonstrates the lambda expression feature of F#
17+
// to display the contents of the list to the console.
18+
names.ForEach(fun s -> printfn "%s" s)
19+
20+
(* This code will produce output similar to the following:
21+
* Bruce
22+
* Alfred
23+
* Tim
24+
* Richard
25+
* Bruce
26+
* Alfred
27+
* Tim
28+
* Richard
29+
*)
30+
//</snippet01>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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="action.fs" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module TestAction1
2+
3+
// <Snippet2>
4+
open System
5+
open System.Windows.Forms
6+
7+
let showWindowsMessage message =
8+
MessageBox.Show message |> ignore
9+
10+
let messageTarget =
11+
Action<string>(
12+
if Environment.GetCommandLineArgs().Length > 1 then
13+
showWindowsMessage
14+
else
15+
printfn "%s"
16+
)
17+
18+
messageTarget.Invoke "Hello, World!"
19+
20+
// </Snippet2>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0-windows</TargetFramework>
6+
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="Action1.fs" />
12+
<Compile Include="Delegate.fs" />
13+
<Compile Include="Lambda.fs" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module TestCustomDelegate
2+
3+
// <Snippet1>
4+
open System
5+
open System.Windows.Forms
6+
7+
type DisplayMessage = delegate of message: string -> unit
8+
9+
let showWindowsMessage message =
10+
MessageBox.Show message |> ignore
11+
12+
let messageTarget =
13+
DisplayMessage(
14+
if Environment.GetCommandLineArgs().Length > 1 then
15+
showWindowsMessage
16+
else
17+
printfn "%s"
18+
)
19+
20+
messageTarget.Invoke "Hello, World!"
21+
22+
// </Snippet1>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module TestLambdaExpression
2+
3+
// <Snippet4>
4+
open System
5+
open System.Windows.Forms
6+
7+
let showWindowsMessage message =
8+
MessageBox.Show message |> ignore
9+
10+
let messageTarget =
11+
Action<string>(
12+
if Environment.GetCommandLineArgs().Length > 1 then
13+
fun s -> showWindowsMessage s
14+
else
15+
fun s -> printfn "%s" s
16+
)
17+
18+
messageTarget.Invoke "Hello, World!"
19+
20+
// </Snippet4>

xml/System/Action`1.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@
6969
7070
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/delegate.cpp" id="Snippet1":::
7171
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Delegate.cs" id="Snippet1":::
72+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs" id="Snippet1":::
7273
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Delegate.vb" id="Snippet1":::
7374
7475
The following example simplifies this code by instantiating the <xref:System.Action%601> delegate instead of explicitly defining a new delegate and assigning a named method to it.
7576
7677
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/action`1.cpp" id="Snippet2":::
7778
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Action1.cs" id="Snippet2":::
79+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs" id="Snippet2":::
7880
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Action1.vb" id="Snippet2":::
7981
8082
You can also use the <xref:System.Action%601> 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).)
@@ -84,6 +86,7 @@
8486
You can also assign a lambda expression to an <xref:System.Action%601> 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).)
8587
8688
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Lambda.cs" id="Snippet4":::
89+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs" id="Snippet4":::
8790
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/lambda.vb" id="Snippet4":::
8891
8992
The <xref:System.Collections.Generic.List%601.ForEach%2A> and <xref:System.Array.ForEach%2A> methods each take an <xref:System.Action%601> delegate as a parameter. The method encapsulated by the delegate allows you to perform an action on each element in the array or list. The example uses the <xref:System.Collections.Generic.List%601.ForEach%2A> method to provide an illustration.
@@ -94,6 +97,7 @@
9497
The following example demonstrates the use of the <xref:System.Action%601> delegate to print the contents of a <xref:System.Collections.Generic.List%601> object. In this example, the `Print` method is used to display the contents of the list to the console. In addition, the C# example also demonstrates the use of anonymous methods to display the contents to the console. Note that the example does not explicitly declare an <xref:System.Action%601> variable. Instead, it passes a reference to a method that takes a single parameter and that does not return a value to the <xref:System.Collections.Generic.List%601.ForEach%2A?displayProperty=nameWithType> method, whose single parameter is an <xref:System.Action%601> delegate. Similarly, in the C# example, an <xref:System.Action%601> delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the <xref:System.Action%601> delegate that is expected by the <xref:System.Collections.Generic.List%601.ForEach%2A?displayProperty=nameWithType> method.
9598
9699
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action_PrintExample/cs/action.cs" interactive="try-dotnet-method" id="Snippet01":::
100+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs" id="Snippet01":::
97101
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action_PrintExample/vb/action.vb" id="Snippet01":::
98102
99103
]]></format>

0 commit comments

Comments
 (0)