|
95 | 95 | > To reference a method that has two parameters and returns `void` (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the generic <xref:System.Action%602> delegate instead. |
96 | 96 | |
97 | 97 | When you use the <xref:System.Func%603> 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 `ExtractMethod` and assigns a reference to the `ExtractWords` method to its delegate instance. |
98 | | - |
99 | | - [!code-csharp[System.Func~3#1](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Delegate.cs#1)] |
100 | | - [!code-vb[System.Func~3#1](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Delegate.vb#1)] |
| 98 | + |
| 99 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1"::: |
| 100 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Delegate.vb" id="Snippet1"::: |
101 | 101 | |
102 | 102 | The following example simplifies this code by instantiating a <xref:System.Func%603> delegate instead of explicitly defining a new delegate and assigning a named method to it. |
103 | | - |
104 | | - [!code-csharp[System.Func~3#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Func3.cs#2)] |
105 | | - [!code-vb[System.Func~3#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Func3.vb#2)] |
| 103 | + |
| 104 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Func3.cs" interactive="try-dotnet" id="Snippet2"::: |
| 105 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Func3.vb" id="Snippet2"::: |
106 | 106 | |
107 | 107 | You can use the <xref:System.Func%603> 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).) |
108 | | - |
109 | | - [!code-csharp[System.Func~3#3](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Anon.cs#3)] |
| 108 | + |
| 109 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Anon.cs" interactive="try-dotnet" id="Snippet3"::: |
110 | 110 | |
111 | 111 | You can also assign a lambda expression to a <xref:System.Func%603> 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).) |
112 | 112 | |
113 | | - [!code-csharp[System.Func~3#4](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Lambda.cs#4)] |
114 | | - [!code-vb[System.Func~3#4](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/lambda.vb#4)] |
115 | | - |
| 113 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4"::: |
| 114 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/lambda.vb" id="Snippet4"::: |
| 115 | +
|
116 | 116 | 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 <xref:System.Func%603> parameters, you can pass these methods a lambda expression without explicitly instantiating a <xref:System.Func%603> delegate. |
117 | 117 | |
118 | 118 | |
119 | 119 | |
120 | 120 | ## Examples |
121 | 121 | The following example demonstrates how to declare and use a <xref:System.Func%603> delegate. This example declares a <xref:System.Func%603> variable and assigns it a lambda expression that takes a <xref:System.String> value and an <xref:System.Int32> value as parameters. The lambda expression returns `true` if the length of the <xref:System.String> parameter is equal to the value of the <xref:System.Int32> parameter. The delegate that encapsulates this method is subsequently used in a query to filter strings in an array of strings. |
122 | | - |
123 | | - [!code-csharp[System.Func~3#5](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Example.cs#5)] |
124 | | - [!code-vb[System.Func~3#5](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb#5)] |
125 | | - |
| 122 | + |
| 123 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Example.cs" interactive="try-dotnet" id="Snippet5"::: |
| 124 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb" id="Snippet5"::: |
| 125 | +
|
126 | 126 | ]]></format> |
127 | 127 | </remarks> |
128 | 128 | <related type="Article" href="/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">Lambda Expressions (C# Programming Guide)</related> |
|
0 commit comments