展开 最小化
此文章由机器翻译。 将光标移到文章的句子上,以查看原文。 更多信息。
译文
原文
此主题尚未评级 - 评价此主题

Action<T1, T2, T3, T4> 委托

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

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)
public delegate void Action<in T1, in T2, in T3, in T4>(
	T1 arg1,
	T2 arg2,
	T3 arg3,
	T4 arg4
)

类型参数

in T1

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

该类型参数是逆变。即可以使用指定的类型或派生程度更低的类型。有关协变和逆变的更多信息,请参见泛型中的协变和逆变

in T2

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

in T3

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

in T4

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

参数

arg1
类型:T1
此委托封装的方法的第一个参数。
arg2
类型:T2
此委托封装的方法的第二个参数。
arg3
类型:T3
此委托封装的方法的第三个参数。
arg4
类型:T4
此委托封装的方法的第四个参数。

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

说明说明

若要引用具有四个参数并返回值的方法,请改用泛型 Func<T1, T2, T3, T4, TResult> 委托。

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


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) ? "<None>" : 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<T1, T2, T3, T4> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。


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<string[], string[], int, int> copyOperation = CopyStrings;
      copyOperation(ordinals, copiedOrdinals, 3, 5);
      foreach (string ordinal in copiedOrdinals)
         Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "<None>" : 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<T1, T2, T3, T4> 委托与匿名方法一起使用。 (有关匿名方法的简介,请参见匿名方法(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<string[], string[], int, int> copyOperation = 
                                           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) ? "<None>" : 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<T1, T2, T3, T4> 委托实例。 (有关 lambda 表达式的简介,请参见 Lambda 表达式(C# 编程指南)。)


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<string[], string[], int, int> copyOperation = (s1, s2, pos, num)
                                           => CopyStrings(s1, s2, pos, num);  
      copyOperation(ordinals, copiedOrdinals, 3, 5);
      foreach (string ordinal in copiedOrdinals)
         Console.WriteLine(String.IsNullOrEmpty(ordinal) ? "<None>" : 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]);
   }
}


.NET Framework

受以下版本支持:4.5、4、3.5

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

可移植类库

受以下版本支持:可移植类库

适用于 Windows 应用商店应用的 .NET

受以下版本支持:Windows 8

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求
本文是否对您有所帮助?
(1500 个剩余字符)

社区附加资源

添加
© 2013 Microsoft. 版权所有。