System.Action 委托
方法描述
封装一个方法,该方法具有四个参数并且不返回值。
语法定义(C# System.Action 委托 的用法)
public delegate void Action( T1 arg1, T2 arg2, T3 arg3, T4 arg4 )
提示和注释
可以使用 Action
注意
若要引用具有四个参数并返回值的方法,请改用泛型 Func
在使用 Action
C#
VB
复制
using System;
delegate void StringCopy(string[] stringArray1,
string[] stringArray2,
int indexToStart,
int numberToCopy);
public class TestDelegate
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
string[] copiedOrdinals = new string[ordinals.Length];
StringCopy copyOperation = CopyStrings;
copyOperation(ordinals, copiedOrdinals, 3, 5);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "
}
private static void CopyStrings(string[] source, string[] target,
int startPos, int number)
{
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 <= startPos + number - 1; ctr++)
target[ctr] = String.Copy(source[ctr]);
}
}
以下示例简化了此代码,它所用的方法是实例化 Action
C#
VB
复制
using System;
public class TestAction4
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
string[] copiedOrdinals = new string[ordinals.Length];
Action
copyOperation(ordinals, copiedOrdinals, 3, 5);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "
}
private static void CopyStrings(string[] source, string[] target,
int startPos, int number)
{
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 <= startPos + number - 1; ctr++)
target[ctr] = String.Copy(source[ctr]);
}
}
您也可以按照以下示例所演示的那样在 C# 中将 Action
C#
复制
using System;
public class TestAnonymousMethod
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
string[] copiedOrdinals = new string[ordinals.Length];
Action
delegate(string[] s1, string[] s2,
int pos, int num)
{ CopyStrings(s1, s2, pos, num); };
copyOperation(ordinals, copiedOrdinals, 3, 5);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "
}
private static void CopyStrings(string[] source, string[] target,
int startPos, int number)
{
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 <= startPos + number - 1; ctr++)
target[ctr] = String.Copy(source[ctr]);
}
}
您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Action
C#
VB
复制
using System;
public class TestLambdaExpression
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
string[] copiedOrdinals = new string[ordinals.Length];
Action
=> CopyStrings(s1, s2, pos, num);
copyOperation(ordinals, copiedOrdinals, 3, 5);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "
}
private static void CopyStrings(string[] source, string[] target,
int startPos, int number)
{
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 <= startPos + number - 1; ctr++)
target[ctr] = String.Copy(source[ctr]);
}
}
System.Action 委托例子
继承层次结构
线程安全
版本信息
.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 系统要求。