|
73 | 73 | <param name="obj">The parameter of the method that this delegate encapsulates.</param>
|
74 | 74 | <summary>Encapsulates a method that has a single parameter and does not return a value.</summary>
|
75 | 75 | <remarks>
|
76 |
| - <format type="text/markdown"><![CDATA[ |
77 |
| -
|
78 |
| -## Remarks |
79 |
| - You can use the <xref:System.Action%601> 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 one parameter that is 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. |
80 |
| -
|
| 76 | + <format type="text/markdown"><![CDATA[ |
| 77 | + |
| 78 | +## Remarks |
| 79 | + You can use the <xref:System.Action%601> 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 one parameter that is 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. |
| 80 | + |
81 | 81 | > [!NOTE]
|
82 |
| -> To reference a method that has one parameter and returns a value, use the generic <xref:System.Func%602> delegate instead. |
83 |
| -
|
84 |
| - When you use the <xref:System.Action%601> delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter. For example, the following code explicitly declares a delegate named `DisplayMessage` and assigns a reference to either the <xref:System.Console.WriteLine%2A> method or the `ShowWindowsMessage` method to its delegate instance. |
85 |
| -
|
| 82 | +> To reference a method that has one parameter and returns a value, use the generic <xref:System.Func%602> delegate instead. |
| 83 | + |
| 84 | + When you use the <xref:System.Action%601> delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter. For example, the following code explicitly declares a delegate named `DisplayMessage` and assigns a reference to either the <xref:System.Console.WriteLine%2A> method or the `ShowWindowsMessage` method to its delegate instance. |
| 85 | + |
| 86 | + :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/delegate.cpp" id="Snippet1"::: |
86 | 87 | :::code language="csharp" source="~/snippets/csharp/System/ActionT/Overview/Delegate.cs" id="Snippet1":::
|
87 | 88 | :::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Delegate.fs" id="Snippet1":::
|
88 |
| - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Delegate.vb" id="Snippet1"::: |
89 |
| -
|
90 |
| - The following example simplifies this code by instantiating the <xref:System.Action%601> delegate instead of explicitly defining a new delegate and assigning a named method to it. |
91 |
| -
|
| 89 | + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Delegate.vb" id="Snippet1"::: |
| 90 | + |
| 91 | + The following example simplifies this code by instantiating the <xref:System.Action%601> delegate instead of explicitly defining a new delegate and assigning a named method to it. |
| 92 | + |
| 93 | + :::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Action~1/cpp/action`1.cpp" id="Snippet2"::: |
92 | 94 | :::code language="csharp" source="~/snippets/csharp/System/ActionT/Overview/Action1.cs" id="Snippet2":::
|
93 | 95 | :::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Action1.fs" id="Snippet2":::
|
94 |
| - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Action1.vb" id="Snippet2"::: |
95 |
| -
|
96 |
| - You can also use the <xref:System.Action%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).) |
97 |
| -
|
98 |
| - :::code language="csharp" source="~/snippets/csharp/System/ActionT/Overview/Anon.cs" id="Snippet3"::: |
99 |
| -
|
100 |
| - You can also assign a lambda expression to an <xref:System.Action%601> 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).) |
101 |
| -
|
| 96 | + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/Action1.vb" id="Snippet2"::: |
| 97 | + |
| 98 | + You can also use the <xref:System.Action%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).) |
| 99 | + |
| 100 | + :::code language="csharp" source="~/snippets/csharp/System/ActionT/Overview/Anon.cs" id="Snippet3"::: |
| 101 | + |
| 102 | + You can also assign a lambda expression to an <xref:System.Action%601> 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).) |
| 103 | + |
102 | 104 | :::code language="csharp" source="~/snippets/csharp/System/ActionT/Overview/Lambda.cs" id="Snippet4":::
|
103 | 105 | :::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_CLR_System/system.Action~1/fs/Lambda.fs" id="Snippet4":::
|
104 |
| - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/lambda.vb" id="Snippet4"::: |
105 |
| -
|
106 |
| - The <xref:System.Collections.Generic.List%601.ForEach%2A> and <xref:System.Array.ForEach%2A> methods each take an <xref:System.Action%601> delegate as a parameter. The method encapsulated by the delegate allows you to perform an action on each element in the array or list. The example uses the <xref:System.Collections.Generic.List%601.ForEach%2A> method to provide an illustration. |
107 |
| -
|
108 |
| -
|
109 |
| -
|
110 |
| -## Examples |
111 |
| - The following example demonstrates the use of the <xref:System.Action%601> delegate to print the contents of a <xref:System.Collections.Generic.List%601> object. In this example, the `Print` method is used to display the contents of the list to the console. In addition, the C# example also demonstrates the use of anonymous methods to display the contents to the console. Note that the example does not explicitly declare an <xref:System.Action%601> variable. Instead, it passes a reference to a method that takes a single parameter and that does not return a value to the <xref:System.Collections.Generic.List%601.ForEach%2A?displayProperty=nameWithType> method, whose single parameter is an <xref:System.Action%601> delegate. Similarly, in the C# example, an <xref:System.Action%601> delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the <xref:System.Action%601> delegate that is expected by the <xref:System.Collections.Generic.List%601.ForEach%2A?displayProperty=nameWithType> method. |
| 106 | + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action~1/vb/lambda.vb" id="Snippet4"::: |
| 107 | + |
| 108 | + The <xref:System.Collections.Generic.List%601.ForEach%2A> and <xref:System.Array.ForEach%2A> methods each take an <xref:System.Action%601> delegate as a parameter. The method encapsulated by the delegate allows you to perform an action on each element in the array or list. The example uses the <xref:System.Collections.Generic.List%601.ForEach%2A> method to provide an illustration. |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +## Examples |
| 113 | + The following example demonstrates the use of the <xref:System.Action%601> delegate to print the contents of a <xref:System.Collections.Generic.List%601> object. In this example, the `Print` method is used to display the contents of the list to the console. In addition, the C# example also demonstrates the use of anonymous methods to display the contents to the console. Note that the example does not explicitly declare an <xref:System.Action%601> variable. Instead, it passes a reference to a method that takes a single parameter and that does not return a value to the <xref:System.Collections.Generic.List%601.ForEach%2A?displayProperty=nameWithType> method, whose single parameter is an <xref:System.Action%601> delegate. Similarly, in the C# example, an <xref:System.Action%601> delegate is not explicitly instantiated because the signature of the anonymous method matches the signature of the <xref:System.Action%601> delegate that is expected by the <xref:System.Collections.Generic.List%601.ForEach%2A?displayProperty=nameWithType> method. |
112 | 114 |
|
113 | 115 | :::code language="csharp" source="~/snippets/csharp/System/ActionT/Overview/action.cs" interactive="try-dotnet-method" id="Snippet01":::
|
114 | 116 | :::code language="fsharp" source="~/snippets/fsharp/VS_Snippets_CLR_System/system.Action_PrintExample/fs/action.fs" id="Snippet01":::
|
115 |
| - :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action_PrintExample/vb/action.vb" id="Snippet01"::: |
116 |
| -
|
| 117 | + :::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Action_PrintExample/vb/action.vb" id="Snippet01"::: |
| 118 | + |
117 | 119 | ]]></format>
|
118 | 120 | </remarks>
|
119 | 121 | <altmember cref="T:System.Func`2" />
|
|
0 commit comments