diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs new file mode 100644 index 00000000000..39a5fccaf9d --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs @@ -0,0 +1,21 @@ +module Action + +// +open System +open System.Windows.Forms + +type Name(name) = + member _.DisplayToConsole() = + printfn "%s" name + + member _.DisplayToWindow() = + MessageBox.Show name |> ignore + +let testName = Name "Koani" + +// unit -> unit functions and methods can be cast to Action. +let showMethod = Action testName.DisplayToWindow + +showMethod.Invoke() + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fsproj new file mode 100644 index 00000000000..427dac272f0 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fsproj @@ -0,0 +1,16 @@ + + + + Exe + net5.0-windows + true + true + + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs new file mode 100644 index 00000000000..58afc2aff75 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs @@ -0,0 +1,21 @@ +module Delegate + +// +open System.Windows.Forms + +type ShowValue = delegate of unit -> unit + +type Name(name) = + member _.DisplayToConsole() = + printfn "%s" name + + member _.DisplayToWindow() = + MessageBox.Show name |> ignore + +let testName = Name "Koani" + +let showMethod = ShowValue testName.DisplayToWindow + +showMethod.Invoke() + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs new file mode 100644 index 00000000000..4937a44ed5b --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs @@ -0,0 +1,20 @@ +module Lambda + +// +open System +open System.Windows.Forms + +type Name(name) = + member _.DisplayToConsole() = + printfn "%s" name + + member _.DisplayToWindow() = + MessageBox.Show name |> ignore + +let testName = Name "Koani" + +let showMethod = Action(fun () -> testName.DisplayToWindow()) + +showMethod.Invoke() + +// diff --git a/xml/System/Action.xml b/xml/System/Action.xml index 4340bc196eb..96c6f047674 100644 --- a/xml/System/Action.xml +++ b/xml/System/Action.xml @@ -62,7 +62,7 @@ [!NOTE] > To reference a method that has no parameters and returns a value, use the generic delegate instead. @@ -71,21 +71,24 @@ :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.action.delegate/cpp/delegate.cpp" id="Snippet1"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.action.delegate/cs/delegate.cs" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.action.delegate/vb/delegate.vb" id="Snippet1"::: The following example simplifies this code by instantiating the 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/cpp/action.cpp" id="Snippet2"::: :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Action.cs" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action/vb/Action.vb" id="Snippet2"::: You can also use the 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).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Anon.cs" id="Snippet3"::: - You can also assign a lambda expression to an 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).) + You can also assign a lambda expression to an 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).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Lambda.cs" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action/vb/lambda.vb" id="Snippet4"::: ]]>