Action<T> 委托
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
封装一个方法,该方法只有一个参数并且不返回值。
generic <typename T>
public delegate void Action(T obj);
public delegate void Action<in T>(T obj);
public delegate void Action<T>(T obj);
type Action<'T> = delegate of 'T -> unit
Public Delegate Sub Action(Of In T)(obj As T)
Public Delegate Sub Action(Of T)(obj As T)
- obj
- T
此委托封装的方法的参数。
以下示例演示如何使用 Action<T> 委托来打印对象的内容 List<T> 。 在此示例中, Print
该方法用于向控制台显示列表的内容。 此外,C# 示例还演示了使用匿名方法向控制台显示内容。 请注意,该示例不显式声明变量 Action<T> 。 相反,它会将引用传递给采用单个参数的方法,并且不向 List<T>.ForEach 该方法返回值,该方法的单个参数是 Action<T> 委托。 同样,在 C# 示例中,Action<T>不会显式实例化委托,因为匿名方法的签名与该方法预期的List<T>.ForEach委托的Action<T>签名匹配。
List<string> names = new List<string>();
names.Add("Bruce");
names.Add("Alfred");
names.Add("Tim");
names.Add("Richard");
// Display the contents of the list using the Print method.
names.ForEach(Print);
// The following demonstrates the anonymous method feature of C#
// to display the contents of the list to the console.
names.ForEach(delegate(string name)
{
Console.WriteLine(name);
});
void Print(string s)
{
Console.WriteLine(s);
}
/* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*/
// F# provides a type alias for System.Collections.List<'T> as ResizeArray<'T>.
let names = ResizeArray<string>()
names.Add "Bruce"
names.Add "Alfred"
names.Add "Tim"
names.Add "Richard"
let print s = printfn "%s" s
// Display the contents of the list using the print function.
names.ForEach(Action<string> print)
// The following demonstrates the lambda expression feature of F#
// to display the contents of the list to the console.
names.ForEach(fun s -> printfn "%s" s)
(* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*)
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim names As New List(Of String)
names.Add("Bruce")
names.Add("Alfred")
names.Add("Tim")
names.Add("Richard")
' Display the contents of the list using the Print method.
names.ForEach(AddressOf Print)
End Sub
Shared Sub Print(ByVal s As String)
Console.WriteLine(s)
End Sub
End Class
' This code will produce output similar to the following:
' Bruce
' Alfred
' Tim
' Richard
可以使用 Action<T> 委托将方法作为参数传递,而无需显式声明自定义委托。 封装的方法必须与此委托定义的方法签名相对应。 这意味着封装方法必须具有一个按值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void
。 在Visual Basic中,它必须由 ... 定义Sub``End Sub
construct。 它也可以是返回忽略的值的方法。) 通常,此类方法用于执行操作。
备注
若要引用具有一个参数并返回值的方法,请改用泛型 Func<T,TResult> 委托。
使用 Action<T> 委托时,无需显式定义封装具有单个参数的方法的委托。 例如,以下代码显式声明命名 DisplayMessage
的委托,并向委托实例分配对 WriteLine 方法或方法的 ShowWindowsMessage
引用。
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
public delegate void DisplayMessage(String^ message);
public ref class TestCustomDelegate
{
public:
static void ShowWindowsMessage(String^ message)
{
MessageBox::Show(message);
}
};
int main()
{
DisplayMessage^ messageTarget;
if (Environment::GetCommandLineArgs()->Length > 1)
messageTarget = gcnew DisplayMessage(&TestCustomDelegate::ShowWindowsMessage);
else
messageTarget = gcnew DisplayMessage(&Console::WriteLine);
messageTarget(L"Hello World!");
return 0;
}
using System;
using System.Windows.Forms;
delegate void DisplayMessage(string message);
public class TestCustomDelegate
{
public static void Main()
{
DisplayMessage messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
open System
open System.Windows.Forms
type DisplayMessage = delegate of message: string -> unit
let showWindowsMessage message =
MessageBox.Show message |> ignore
let messageTarget =
DisplayMessage(
if Environment.GetCommandLineArgs().Length > 1 then
showWindowsMessage
else
printfn "%s"
)
messageTarget.Invoke "Hello, World!"
Delegate Sub DisplayMessage(message As String)
Module TestCustomDelegate
Public Sub Main
Dim messageTarget As DisplayMessage
If Environment.GetCommandLineArgs().Length > 1 Then
messageTarget = AddressOf ShowWindowsMessage
Else
messageTarget = AddressOf Console.WriteLine
End If
messageTarget("Hello, World!")
End Sub
Private Sub ShowWindowsMessage(message As String)
MsgBox(message)
End Sub
End Module
以下示例通过实例化 Action<T> 委托而不是显式定义新委托并向其分配命名方法来简化此代码。
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
namespace ActionExample
{
public ref class Message
{
public:
static void ShowWindowsMessage(String^ message)
{
MessageBox::Show(message);
}
};
}
int main()
{
Action<String^>^ messageTarget;
if (Environment::GetCommandLineArgs()->Length > 1)
messageTarget = gcnew Action<String^>(&ActionExample::Message::ShowWindowsMessage);
else
messageTarget = gcnew Action<String^>(&Console::WriteLine);
messageTarget("Hello, World!");
return 0;
}
using System;
using System.Windows.Forms;
public class TestAction1
{
public static void Main()
{
Action<string> messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
open System
open System.Windows.Forms
let showWindowsMessage message =
MessageBox.Show message |> ignore
let messageTarget =
Action<string>(
if Environment.GetCommandLineArgs().Length > 1 then
showWindowsMessage
else
printfn "%s"
)
messageTarget.Invoke "Hello, World!"
Module TestAction1
Public Sub Main
Dim messageTarget As Action(Of String)
If Environment.GetCommandLineArgs().Length > 1 Then
messageTarget = AddressOf ShowWindowsMessage
Else
messageTarget = AddressOf Console.WriteLine
End If
messageTarget("Hello, World!")
End Sub
Private Sub ShowWindowsMessage(message As String)
MsgBox(message)
End Sub
End Module
还可以将 Action<T> 委托与 C# 中的匿名方法一起使用,如以下示例所示。 (有关匿名方法简介,请参阅 Anonymous Methods.)
using System;
using System.Windows.Forms;
public class TestAnonMethod
{
public static void Main()
{
Action<string> messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = delegate(string s) { ShowWindowsMessage(s); };
else
messageTarget = delegate(string s) { Console.WriteLine(s); };
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
还可以将 lambda 表达式分配给 Action<T> 委托实例,如以下示例所示。 (有关 lambda 表达式简介,请参阅 Lambda Expressions.)
using System;
using System.Windows.Forms;
public class TestLambdaExpression
{
public static void Main()
{
Action<string> messageTarget;
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = s => ShowWindowsMessage(s);
else
messageTarget = s => Console.WriteLine(s);
messageTarget("Hello, World!");
}
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
open System
open System.Windows.Forms
let showWindowsMessage message =
MessageBox.Show message |> ignore
let messageTarget =
Action<string>(
if Environment.GetCommandLineArgs().Length > 1 then
fun s -> showWindowsMessage s
else
fun s -> printfn "%s" s
)
messageTarget.Invoke "Hello, World!"
Imports System.Windows.Forms
Public Module TestLambdaExpression
Public Sub Main()
Dim messageTarget As Action(Of String)
If Environment.GetCommandLineArgs().Length > 1 Then
messageTarget = Sub(s) ShowWindowsMessage(s)
Else
messageTarget = Sub(s) ShowConsoleMessage(s)
End If
messageTarget("Hello, World!")
End Sub
Private Function ShowWindowsMessage(message As String) As Integer
Return MessageBox.Show(message)
End Function
Private Function ShowConsoleMessage(message As String) As Integer
Console.WriteLine(message)
Return 0
End Function
End Module
每个 ForEach 委托作为参数的和 ForEach 方法 Action<T> 。 委托封装的方法允许对数组或列表中的每个元素执行操作。 该示例使用 ForEach 该方法提供插图。
Get |
获取指示指定委托表示的方法的对象。 |