diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs new file mode 100644 index 00000000000..ef1f576a0cf --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs @@ -0,0 +1,27 @@ +module TestLambdaExpression + +// +open System +open System.IO + +let message1 = "The first line of a message" +let message2 = "The second line of a message" + +let writeToConsole string1 string2 = + printfn $"{string1}\n{string2}" + +let writeToFile string1 string2 = + use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) + writer.WriteLine $"{string1}\n{string2}" + +let concat = + Action(fun string1 string2 -> + if Environment.GetCommandLineArgs().Length > 1 then + writeToFile string1 string2 + else + writeToConsole string1 string2 + ) + +concat.Invoke(message1, message2) + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fsproj new file mode 100644 index 00000000000..68850b7e7c3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fsproj @@ -0,0 +1,13 @@ + + + + Exe + net5.0 + + + + + + + + diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs new file mode 100644 index 00000000000..ecedcc7c4b8 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs @@ -0,0 +1,29 @@ +module TestDelegate + +// +open System +open System.IO + +type ConcatStrings = delegate of string1: string * string1: string -> unit + +let message1 = "The first line of a message" +let message2 = "The second line of a message" + +let writeToConsole string1 string2 = + printfn $"{string1}\n{string2}" + +let writeToFile string1 string2 = + use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) + writer.WriteLine $"{string1}\n{string2}" + +let concat = + ConcatStrings(fun string1 string2 -> + if Environment.GetCommandLineArgs().Length > 1 then + writeToFile string1 string2 + else + writeToConsole string1 string2 + ) + +concat.Invoke(message1, message2) + +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs new file mode 100644 index 00000000000..e3b9053b647 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs @@ -0,0 +1,27 @@ +module TestDelegate + +// +open System +open System.IO + +let message1 = "The first line of a message" +let message2 = "The second line of a message" + +let writeToConsole string1 string2 = + printfn $"{string1}\n{string2}" + +let writeToFile string1 string2 = + use writer = new StreamWriter(Environment.GetCommandLineArgs().[1], false) + writer.WriteLine $"{string1}\n{string2}" + +let concat = + Action( + if Environment.GetCommandLineArgs().Length > 1 then + fun s1 s2 -> writeToFile s1 s2 + else + fun s1 s2 -> writeToConsole s1 s2 + ) + +concat.Invoke(message1, message2) + +// diff --git a/xml/System/Action`2.xml b/xml/System/Action`2.xml index deb691613c0..4a485cf08b5 100644 --- a/xml/System/Action`2.xml +++ b/xml/System/Action`2.xml @@ -84,7 +84,7 @@ 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 two parameters that are both 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. + You can use the 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 two parameters that are both 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. > [!NOTE] > To reference a method that has two parameters and returns a value, use the generic delegate instead. @@ -92,20 +92,23 @@ When you use the delegate, you do not have to explicitly define a delegate that encapsulates a method with two parameters. For example, the following code explicitly declares a delegate named `ConcatStrings`. It then assigns a reference to either of two methods to its delegate instance. One method writes two strings to the console; the second writes two strings to a file. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/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="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action~2/cs/Action2.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Action2.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/action2.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~2/cs/Anon.cs" interactive="try-dotnet" 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~2/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action~2/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~2/vb/lambda.vb" id="Snippet4"::: ]]>