来源:https://docs.microsoft.com/zh-cn/dotnet/api/system.action-1?view=netframework-4.7.2
Action<T> Delegate
定义
封装一个方法,该方法只有一个参数并且不返回值。
public delegate void Action<in T>(T obj);
类型参数
- T
此委托封装的方法的参数类型。
参数
- obj
此委托封装的方法的参数。
- 继承
示例
下面的示例演示如何将Action<T>要打印的内容委托List<T>对象。 在此示例中,Print
方法用于向控制台显示列表的内容。 此外,C# 示例还演示如何使用匿名方法来将内容显示到控制台。 请注意,该示例不显式声明Action<T>变量。 相反,它将传递到方法采用单个参数和与未返回到的值的引用List<T>.ForEach方法,其单个参数是Action<T>委托。 同样,在 C# 示例中,Action<T>委托不显式实例化,因为匿名方法的签名匹配的签名Action<T>委托所需的List<T>.ForEach方法。
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
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);
});
}
private static void Print(string s)
{
Console.WriteLine(s);
}
}
/* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*/
注解
可以使用Action<T>委托作为参数传递方法,而无需显式声明自定义委托。 封装的方法必须对应于此委托定义的方法签名。 这意味着,封装的方法必须具有按值传递给它的一个参数,并且不能返回值。 (在 C# 中,该方法必须返回void
。 在 Visual Basic 中,它必须由定义Sub
...End Sub
构造。 它也可以是返回一个值,则忽略该值的方法。)通常情况下,这种方法用于执行操作。
备注
若要引用的方法,具有一个参数并返回一个值,请使用泛型Func<T,TResult>改为委托。
当你使用Action<T>委托时,您无需显式定义用于封装具有单个参数的方法的委托。 例如,下面的代码显式声明名为的委托DisplayMessage
,并将分配到的引用WriteLine方法或ShowWindowsMessage
给其委托实例的方法。
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);
}
}
下面的示例简化了此代码实例化Action<T>而不是显式定义一个新委托,并为其赋值命名的方法的委托。
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);
}
}
此外可以使用Action<T>委托与匿名方法在 C# 中,如以下示例所示。 (有关匿名方法的介绍,请参阅匿名方法。)
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);
}
}
ForEach并ForEach每个方法均采用Action<T>委托作为参数。 由委托封装的方法,可对数组或列表中的每个元素执行操作。 该示例使用ForEach方法来提供演示。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2013-12-02 在ASP.NET应用程序中使用身份模拟(Impersonation)