System.Action 委托

上一篇:System.Action 类 下一篇:System.Action 类

方法描述

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

语法定义(C# System.Action 委托 的用法)

public delegate void Action(
	T1 arg1,
	T2 arg2,
	T3 arg3
)

构造函数

构造函数名称 构造函数描述

成员/方法

方法名称 方法描述

提示和注释

可以使用 Action 委托以参数形式传递方法,而不用显式声明自定义的委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有三个均通过值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void。 在 Visual Basic 中,必须通过 Sub…End Sub 结构来定义它。 它也可以是返回已忽略的值的方法。)通常,这种方法用于执行某个操作。

注意

若要引用具有三个参数并返回值的方法,请改用泛型 Func 委托。

在使用 Action 委托时,不必显式定义一个封装具有三个参数的方法的委托。 例如,以下代码显式声明了一个名为 StringCopy 的委托,并将对 CopyStrings 方法的引用分配给其委托实例。

C#

VB

复制

using System;

delegate void StringCopy(string[] stringArray1,

string[] stringArray2,

int indexToStart);

public class TestDelegate

{

public static void Main()

{

string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};

string[] copiedOrdinals = new string[ordinals.Length];

StringCopy copyOperation = CopyStrings;

copyOperation(ordinals, copiedOrdinals, 3);

foreach (string ordinal in copiedOrdinals)

Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "" : ordinal);

}

private static void CopyStrings(string[] source, string[] target, int startPos)

{

if (source.Length != target.Length)

throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

for (int ctr = startPos; ctr <= source.Length - 1; ctr++)

target[ctr] = String.Copy(source[ctr]);

}

}

以下示例简化了此代码,它所用的方法是实例化 Action 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

C#

VB

复制

using System;

public class TestAction3

{

public static void Main()

{

string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};

string[] copiedOrdinals = new string[ordinals.Length];

Action copyOperation = CopyStrings;

copyOperation(ordinals, copiedOrdinals, 3);

foreach (string ordinal in copiedOrdinals)

Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "" : ordinal);

}

private static void CopyStrings(string[] source, string[] target, int startPos)

{

if (source.Length != target.Length)

throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

for (int ctr = startPos; ctr <= source.Length - 1; ctr++)

target[ctr] = String.Copy(source[ctr]);

}

}

您也可以按照以下示例所演示的那样在 C# 中将 Action 委托与匿名方法一起使用。 (有关匿名方法的简介,请参见匿名方法(C# 编程指南)。)

C#

复制

using System;

public class TestAnon

{

public static void Main()

{

string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};

string[] copiedOrdinals = new string[ordinals.Length];

Action copyOperation = delegate(string[] s1,

string[] s2,

int pos)

{ CopyStrings(s1, s2, pos); };

copyOperation(ordinals, copiedOrdinals, 3);

foreach (string ordinal in copiedOrdinals)

Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "" : ordinal);

}

private static void CopyStrings(string[] source, string[] target, int startPos)

{

if (source.Length != target.Length)

throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

for (int ctr = startPos; ctr <= source.Length - 1; ctr++)

target[ctr] = String.Copy(source[ctr]);

}

}

您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Action 委托实例。 (有关 lambda 表达式的简介,请参见 Lambda 表达式(C# 编程指南)。)

C#

VB

复制

using System;

public class TestLambda

{

public static void Main()

{

string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};

string[] copiedOrdinals = new string[ordinals.Length];

Action copyOperation = (s1, s2, pos) =>

CopyStrings(s1, s2, pos);

copyOperation(ordinals, copiedOrdinals, 3);

foreach (string ordinal in copiedOrdinals)

Console.WriteLine(ordinal == string.Empty ? "" : ordinal);

}

private static void CopyStrings(string[] source, string[] target, int startPos)

{

if (source.Length != target.Length)

throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

for (int ctr = startPos; ctr <= source.Length - 1; ctr++)

target[ctr] = String.Copy(source[ctr]);

}

}

System.Action 委托例子


继承层次结构

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

线程安全

版本信息

.NET Framework 受以下版本支持:4、3.5 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。

上一篇:System.Action 类 下一篇:System.Action 类