|
77 | 77 | You can use this delegate to represent a method that can be passed 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 must return a value. |
78 | 78 | |
79 | 79 | > [!NOTE] |
80 | | -> To reference a method that has no parameters and returns `void` (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the <xref:System.Action> delegate instead. |
| 80 | +> To reference a method that has no parameters and returns `void` (`unit`, in F#) (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the <xref:System.Action> delegate instead. |
81 | 81 | |
82 | 82 | When you use the <xref:System.Func%601> delegate, you do not have to explicitly define a delegate that encapsulates a parameterless method. For example, the following code explicitly declares a delegate named `WriteMethod` and assigns a reference to the `OutputTarget.SendToFile` instance method to its delegate instance. |
83 | 83 | |
84 | 84 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~1/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1"::: |
| 85 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~1/fs/Delegate.fs" id="Snippet1"::: |
85 | 86 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~1/vb/Delegate.vb" id="Snippet1"::: |
86 | 87 | |
87 | 88 | The following example simplifies this code by instantiating the <xref:System.Func%601> delegate instead of explicitly defining a new delegate and assigning a named method to it. |
88 | 89 | |
89 | 90 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~1/cs/Func1.cs" interactive="try-dotnet" id="Snippet2"::: |
| 91 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~1/fs/Func1.fs" id="Snippet2"::: |
90 | 92 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~1/vb/Func1.vb" id="Snippet2"::: |
91 | 93 | |
92 | 94 | You can use the <xref:System.Func%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).) |
93 | 95 | |
94 | 96 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~1/cs/Anon.cs" interactive="try-dotnet" id="Snippet3"::: |
95 | 97 | |
96 | | - You can also assign a lambda expression to a <xref:System.Func%602> delegate, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions](/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions) and [Lambda Expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).) |
| 98 | + You can also assign a lambda expression to a <xref:System.Func%602> delegate, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions (VB)](/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions), [Lambda Expressions (C#)](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions), and [Lambda Expressions (F#)](/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword/).) |
97 | 99 | |
98 | 100 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~1/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4"::: |
| 101 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~1/fs/Lambda.fs" id="Snippet4"::: |
99 | 102 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~1/vb/Lambda.vb" id="Snippet4"::: |
100 | 103 | |
101 | 104 | The underlying type of a lambda expression is one of the generic `Func` delegates. This makes it possible to pass a lambda expression as a parameter without explicitly assigning it to a delegate. In particular, because many methods of types in the <xref:System.Linq> namespace have `Func` parameters, you can pass these methods a lambda expression without explicitly instantiating a `Func` delegate. |
|
110 | 113 | The example creates two methods and instantiates two `LazyValue` objects with lambda expressions that call these methods. The lambda expressions do not take parameters because they just need to call a method. As the output shows, the two methods are executed only when the value of each `LazyValue` object is retrieved. |
111 | 114 | |
112 | 115 | :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~1/cs/Example.cs" interactive="try-dotnet" id="Snippet5"::: |
| 116 | + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~1/fs/Example.fs" id="Snippet5"::: |
113 | 117 | :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~1/vb/Example.vb" id="Snippet5"::: |
114 | 118 | |
115 | 119 | ]]></format> |
116 | 120 | </remarks> |
117 | 121 | <related type="Article" href="/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">Lambda Expressions (C# Programming Guide)</related> |
| 122 | + <related type="Article" href="/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword">Lambda Expressions: The fun Keyword (F#)</related> |
118 | 123 | <related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions">Lambda Expressions</related> |
119 | 124 | <related type="Article" href="/dotnet/csharp/programming-guide/delegates/">Delegates (C# Programming Guide)</related> |
| 125 | + <related type="Article" href="/dotnet/fsharp/language-reference/delegates/">Delegates (F#)</related> |
120 | 126 | <related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/delegates/">Delegates in Visual Basic</related> |
121 | 127 | </Docs> |
122 | 128 | </Type> |
0 commit comments