|
102 | 102 |
|
103 | 103 | When you use the <xref:System.Func%604> delegate, you do not have to explicitly define a delegate that encapsulates a method with three parameters. For example, the following code explicitly declares a generic delegate named `ParseNumber` and assigns a reference to the <xref:System.Int32.Parse%2A> method to its delegate instance.
|
104 | 104 |
|
105 |
| - [!code-csharp[System.Func~4#1](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Delegate.cs#1)] |
106 |
| - [!code-vb[System.Func~4#1](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Delegate.vb#1)] |
| 105 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1"::: |
| 106 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Delegate.vb" id="Snippet1"::: |
107 | 107 |
|
108 | 108 | The following example simplifies this code by instantiating the <xref:System.Func%604> delegate instead of explicitly defining a new delegate and assigning a named method to it.
|
109 | 109 |
|
110 |
| - [!code-csharp[System.Func~4#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Func4.cs#2)] |
111 |
| - [!code-vb[System.Func~4#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Func4.vb#2)] |
| 110 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Func4.cs" interactive="try-dotnet" id="Snippet2"::: |
| 111 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Func4.vb" id="Snippet2"::: |
112 | 112 |
|
113 | 113 | You can use the <xref:System.Func%604> 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).)
|
114 | 114 |
|
115 |
| - [!code-csharp[System.Func~4#3](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Anon.cs#3)] |
| 115 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Anon.cs" interactive="try-dotnet" id="Snippet3"::: |
116 | 116 |
|
117 | 117 | You can also assign a lambda expression to a <xref:System.Func%604> 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).)
|
118 | 118 |
|
119 |
| - [!code-csharp[System.Func~4#4](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Lambda.cs#4)] |
120 |
| - [!code-vb[System.Func~4#4](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Lambda.vb#4)] |
| 119 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~4/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4"::: |
| 120 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Lambda.vb" id="Snippet4"::: |
121 | 121 |
|
122 | 122 | 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.
|
123 | 123 |
|
|
126 | 126 | ## Examples
|
127 | 127 | 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.
|
128 | 128 |
|
129 |
| - [!code-csharp[System.Func~3#5](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Example.cs#5)] |
130 |
| - [!code-vb[System.Func~3#5](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb#5)] |
| 129 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Example.cs" interactive="try-dotnet" id="Snippet5"::: |
| 130 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb" id="Snippet5"::: |
131 | 131 |
|
132 | 132 | ]]></format>
|
133 | 133 | </remarks>
|
|
0 commit comments