来源:https://docs.microsoft.com/zh-cn/dotnet/api/system.action-1?view=netframework-4.7.2

Action<T> Delegate

定义

命名空间:
System
Assemblies:
System.Runtime.dll, mscorlib.dll, netstandard.dll

封装一个方法,该方法只有一个参数并且不返回值。

C#复制
public delegate void Action<in T>(T obj);

类型参数

T

此委托封装的方法的参数类型。

参数

obj

此委托封装的方法的参数。

继承

Action<T>

示例

下面的示例演示如何将Action<T>要打印的内容委托List<T>对象。 在此示例中,Print方法用于向控制台显示列表的内容。 此外,C# 示例还演示如何使用匿名方法来将内容显示到控制台。 请注意,该示例不显式声明Action<T>变量。 相反,它将传递到方法采用单个参数和与未返回到的值的引用List<T>.ForEach方法,其单个参数是Action<T>委托。 同样,在 C# 示例中,Action<T>委托不显式实例化,因为匿名方法的签名匹配的签名Action<T>委托所需的List<T>.ForEach方法。

C#复制
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给其委托实例的方法。

C#复制
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>而不是显式定义一个新委托,并为其赋值命名的方法的委托。

C#复制
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# 中,如以下示例所示。 (有关匿名方法的介绍,请参阅匿名方法。)

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 表达式。)

C#复制
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);
}
}

ForEachForEach每个方法均采用Action<T>委托作为参数。 由委托封装的方法,可对数组或列表中的每个元素执行操作。 该示例使用ForEach方法来提供演示。

适用于

.NET Core

2.1 2.0 1.1 1.0

.NET Framework

4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0

.NET Standard

2.0 1.6 1.5 1.4 1.3 1.2 1.1 1.0

Xamarin.Android

7.1

Xamarin.iOS

10.8

Xamarin.Mac

3.0

另请参阅

Action<T> Delegate的更多相关文章

  1. 处理跨线程操作问题(使用Action和delegate或lambda表达式)

    方法A: Action f = () =>                    {                       txtProcess.Text = "开始更新程序.. ...

  2. C# 匿名方法 委托 Action委托 Delegate委托

    原文地址:https://msdn.microsoft.com/zh-cn/library/bb882516.aspx 匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿 ...

  3. 关于Action<T> 、Func<T>、EventHandler<T>、event、delegate

    c# 最初的时候 只有 delegate,之后的版本封装了Action<T> .Func<T>.EventHandler<T> 关于Action<T>  ...

  4. C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别

    以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...

  5. 浅谈C#中常见的委托<Func,Action,Predicate>(转)

    一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...

  6. action func用法记记

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void s ...

  7. C#系统委托之Action And Func

    Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...

  8. Func<T>与Action<T>委托泛型介绍:转

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

  9. Func<T>与Action<T>委托泛型介绍

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

随机推荐

  1. Win7+Ubuntu双系统时间不一致

    转自:http://blog.sina.com.cn/s/blog_55546df90100xkf3.html 最近装了ubuntu和win7双系统,但是发现每次进入win7后时间总是不对,总是比当地 ...

  2. 【转载】 5G+边缘计算,着眼可见的未来 【边缘计算】

    原文地址: https://www.cnblogs.com/upyun/p/10641489.html ------------------------------------------------ ...

  3. 将Myeclipse非maven项目,导入到IDEA

    # 将Myeclipse非maven项目,导入到IDEA 1. 打开原项目,复制“.classpath”文件路径,在IDEA中打开项目时,选此文件路径 2. 进入项目转换界面,默认一步步完成 3. 导 ...

  4. SyntaxError: Non-UTF-8 code starting with '\xe5' in file ***.py on line 105, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for

    用charles抓包时, 对抓到的html,放到pycharm中解析, 结果报错: SyntaxError: Non-UTF-8 code starting with '\xe5' in file * ...

  5. Gym.101955: Asia Shenyang Regional Contest(寒假自训第10场)

    C.Insertion Sort 题意:Q次询问,每次给出N,M,Mod,问你有多少种排列,满足前面M个数字排序之后整个序列的LIS>=N-1. 思路:我们把数字看成[1,M],[N-M+1,N ...

  6. sed 等相关的复习

    sed相打印两行之间的内容: sed -n '/111/,/aad/p' fuxi.txt grep -n ".*" fuxi.txt sed -n '2,9'p fuxi.txt ...

  7. qduoj LC的课后辅导

    描述 有一天,LC给我们出了一道题,如图: 这个图形从左到右由若干个 宽为1 高不确定 的小矩形构成,求出这个图形所包含的最大矩形面积. 输入 多组测试数据每组测试数据的第一行为n(0 <= n ...

  8. acm 2032

    ////////////////////////////////////////////////////////////////////////////////#include<iostream ...

  9. (11)线程池(最新的concurrent.futures包去开启)

    '''concurrent.futures是最新的开启线程池的包'''import timefrom concurrent.futures import ThreadPoolExecutor #开启线 ...

  10. AtCoder Grand Contest 031 B - Reversi

    https://atcoder.jp/contests/agc031/tasks/agc031_b B - Reversi Time Limit: 2 sec / Memory Limit: 1024 ...