Skip to content

Commit 7510963

Browse files
authored
Add F# snippets for System.Action (dotnet#7352)
* Add F# snippets for System.Action * update snippets * Update Action.xml
1 parent 6a66270 commit 7510963

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Action
2+
3+
// <Snippet2>
4+
open System
5+
open System.Windows.Forms
6+
7+
type Name(name) =
8+
member _.DisplayToConsole() =
9+
printfn "%s" name
10+
11+
member _.DisplayToWindow() =
12+
MessageBox.Show name |> ignore
13+
14+
let testName = Name "Koani"
15+
16+
// unit -> unit functions and methods can be cast to Action.
17+
let showMethod = Action testName.DisplayToWindow
18+
19+
showMethod.Invoke()
20+
21+
// </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="Action.fs" />
12+
<Compile Include="Delegate.fs" />
13+
<Compile Include="Lambda.fs" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Delegate
2+
3+
// <Snippet1>
4+
open System.Windows.Forms
5+
6+
type ShowValue = delegate of unit -> unit
7+
8+
type Name(name) =
9+
member _.DisplayToConsole() =
10+
printfn "%s" name
11+
12+
member _.DisplayToWindow() =
13+
MessageBox.Show name |> ignore
14+
15+
let testName = Name "Koani"
16+
17+
let showMethod = ShowValue testName.DisplayToWindow
18+
19+
showMethod.Invoke()
20+
21+
// </Snippet1>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Lambda
2+
3+
// <Snippet4>
4+
open System
5+
open System.Windows.Forms
6+
7+
type Name(name) =
8+
member _.DisplayToConsole() =
9+
printfn "%s" name
10+
11+
member _.DisplayToWindow() =
12+
MessageBox.Show name |> ignore
13+
14+
let testName = Name "Koani"
15+
16+
let showMethod = Action(fun () -> testName.DisplayToWindow())
17+
18+
showMethod.Invoke()
19+
20+
// </Snippet4>

xml/System/Action.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
<format type="text/markdown"><![CDATA[
6363
6464
## Remarks
65-
You can use this 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 no parameters and no return 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.
65+
You can use this 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 no parameters and no return value. (In C#, the method must return `void`. In F# the function or method 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.
6666
6767
> [!NOTE]
6868
> To reference a method that has no parameters and returns a value, use the generic <xref:System.Func%601> delegate instead.
@@ -71,21 +71,24 @@
7171
7272
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.action.delegate/cpp/delegate.cpp" id="Snippet1":::
7373
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.action.delegate/cs/delegate.cs" id="Snippet1":::
74+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Delegate.fs" id="Snippet1":::
7475
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.action.delegate/vb/delegate.vb" id="Snippet1":::
7576
7677
The following example simplifies this code by instantiating the <xref:System.Action> delegate instead of explicitly defining a new delegate and assigning a named method to it.
7778
7879
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Action/cpp/action.cpp" id="Snippet2":::
7980
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Action.cs" id="Snippet2":::
81+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Action.fs" id="Snippet2":::
8082
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action/vb/Action.vb" id="Snippet2":::
8183
8284
You can also use the <xref:System.Action> 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).)
8385
8486
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Anon.cs" id="Snippet3":::
8587
86-
You can also assign a lambda expression to an <xref:System.Action> 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).)
88+
You can also assign a lambda expression to an <xref:System.Action> 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).)
8789
8890
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Action/cs/Lambda.cs" id="Snippet4":::
91+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Action/fs/Lambda.fs" id="Snippet4":::
8992
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Action/vb/lambda.vb" id="Snippet4":::
9093
9194
]]></format>

0 commit comments

Comments
 (0)