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

Skip to content

Add System.Action<T> F# snippets #7380

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 3 commits into from
Nov 12, 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,30 @@
open System

//<snippet01>
// F# provides a type alias for System.Collections.List<'T> as ResizeArray<'T>.
let names = ResizeArray<string>()
names.Add "Bruce"
names.Add "Alfred"
names.Add "Tim"
names.Add "Richard"

let print s = printfn "%s" s

// Display the contents of the list using the print function.
names.ForEach(Action<string> print)

// The following demonstrates the lambda expression feature of F#
// to display the contents of the list to the console.
names.ForEach(fun s -> printfn "%s" s)

(* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*)
//</snippet01>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<Compile Include="action.fs" />
</ItemGroup>

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

// <Snippet2>
open System
open System.Windows.Forms

let showWindowsMessage message =
MessageBox.Show message |> ignore

let messageTarget =
Action<string>(
if Environment.GetCommandLineArgs().Length > 1 then
showWindowsMessage
else
printfn "%s"
)

messageTarget.Invoke "Hello, World!"

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<Compile Include="Action1.fs" />
<Compile Include="Delegate.fs" />
<Compile Include="Lambda.fs" />
</ItemGroup>

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

// <Snippet1>
open System
open System.Windows.Forms

type DisplayMessage = delegate of message: string -> unit

let showWindowsMessage message =
MessageBox.Show message |> ignore

let messageTarget =
DisplayMessage(
if Environment.GetCommandLineArgs().Length > 1 then
showWindowsMessage
else
printfn "%s"
)

messageTarget.Invoke "Hello, World!"

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

// <Snippet4>
open System
open System.Windows.Forms

let showWindowsMessage message =
MessageBox.Show message |> ignore

let messageTarget =
Action<string>(
if Environment.GetCommandLineArgs().Length > 1 then
fun s -> showWindowsMessage s
else
fun s -> printfn "%s" s
)

messageTarget.Invoke "Hello, World!"

// </Snippet4>
4 changes: 4 additions & 0 deletions xml/System/Action`1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/delegate.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Delegate.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Delegate.vb" id="Snippet1":::

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.

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/action`1.cpp" id="Snippet2":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Action1.cs" id="Snippet2":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs" id="Snippet2":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Action1.vb" id="Snippet2":::

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).)
Expand All @@ -84,6 +86,7 @@
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).)

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~1/cs/Lambda.cs" id="Snippet4":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs" id="Snippet4":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/lambda.vb" id="Snippet4":::

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.
Expand All @@ -94,6 +97,7 @@
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.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action_PrintExample/cs/action.cs" interactive="try-dotnet-method" id="Snippet01":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs" id="Snippet01":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action_PrintExample/vb/action.vb" id="Snippet01":::

]]></format>
Expand Down