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 中,它必須由 ... End Sub
建構定義 Sub
。它也可以是傳回忽略值的方法。) 一般而言,這類方法是用來執行作業。
注意
若要參考具有一個參數並傳回值的方法,請改用泛型 Func<T,TResult> 委派。
當您使用委派時 Action<T> ,不需要明確定義以單一參數封裝方法的委派。 例如,下列程式碼會明確宣告名為 DisplayMessage
的委派,並將方法的 ShowWindowsMessage
參考 WriteLine 指派給其委派實例。
#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 運算式.)
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
和 ForEachForEach 方法各採用 Action<T> 委派做為參數。 委派所封裝的方法可讓您對陣列或清單中的每個元素執行動作。 此範例會 ForEach 使用 方法來提供圖例。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |