diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs new file mode 100644 index 00000000000..c155903b5b5 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs @@ -0,0 +1,30 @@ +open System + +// +// F# provides a type alias for System.Collections.List<'T> as ResizeArray<'T>. +let names = ResizeArray() +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 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 +*) +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fsproj new file mode 100644 index 00000000000..678e8a172f3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fsproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs new file mode 100644 index 00000000000..be9f5e90a42 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs @@ -0,0 +1,20 @@ +module TestAction1 + +// +open System +open System.Windows.Forms + +let showWindowsMessage message = + MessageBox.Show message |> ignore + +let messageTarget = + Action( + if Environment.GetCommandLineArgs().Length > 1 then + showWindowsMessage + else + printfn "%s" + ) + +messageTarget.Invoke "Hello, World!" + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fsproj new file mode 100644 index 00000000000..03c098f7086 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fsproj @@ -0,0 +1,16 @@ + + + + Exe + net5.0-windows + true + true + + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs new file mode 100644 index 00000000000..1e8443c0f59 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs @@ -0,0 +1,22 @@ +module TestCustomDelegate + +// +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!" + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs new file mode 100644 index 00000000000..6989f4e5eda --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs @@ -0,0 +1,20 @@ +module TestLambdaExpression + +// +open System +open System.Windows.Forms + +let showWindowsMessage message = + MessageBox.Show message |> ignore + +let messageTarget = + Action( + if Environment.GetCommandLineArgs().Length > 1 then + fun s -> showWindowsMessage s + else + fun s -> printfn "%s" s + ) + +messageTarget.Invoke "Hello, World!" + +// diff --git a/xml/System/Action`1.xml b/xml/System/Action`1.xml index b6dd1282761..631be3166ba 100644 --- a/xml/System/Action`1.xml +++ b/xml/System/Action`1.xml @@ -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 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 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 @@ 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).) :::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 and methods each take an 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 method to provide an illustration. @@ -94,6 +97,7 @@ The following example demonstrates the use of the delegate to print the contents of a 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 variable. Instead, it passes a reference to a method that takes a single parameter and that does not return a value to the method, whose single parameter is an delegate. Similarly, in the C# example, an delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the delegate that is expected by the 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"::: ]]>