zoukankan      html  css  js  c++  java
  • Action<T> 委托

    .NET Framework 4
     

    更新:2010 年 6 月

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

    命名空间:  System
    程序集:  mscorlib(在 mscorlib.dll 中)
     
    public delegate void Action<in T>(
    	T obj
    )
    
    

    类型参数

    in T

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

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

    参数

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

    可以使用 Action<T> 委托以参数形式传递方法,而不用显式声明自定义的委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void 在 Visual Basic 中,必须通过 SubEnd 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);      
       }
    }
    
    
    

    您也可以按照以下示例所演示的那样在 C# 中将 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 表达式(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);      
       }
    }
    
    
    

    ForEach 和 ForEach<T> 方法都采用 Action<T> 委托作为参数。 通过使用由委托封装的方法,可以对数组或列表中的每个元素执行操作。 此示例使用 ForEach 方法提供说明。

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

     
    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
     */
    
    
    

    .NET Framework

    受以下版本支持:4、3.5、3.0、2.0

    .NET Framework Client Profile

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

     

    受以下版本支持:

    Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2

    .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求
     

    Date

    修订记录

    原因

    2010 年 6 月

    修改了要使用 Sub 关键字的 Visual Basic lambda 表达式。

  • 相关阅读:
    poj 3625 Building Roads(最小生成树,二维坐标,基础)
    poj 2031 Building a Space Station(最小生成树,三维,基础)
    poj 2485 Highways(最小生成树,基础,最大边权)
    POJ 2349 Arctic Network(最小生成树,第k大边权,基础)
    hdu 1242 Rescue(BFS,优先队列,基础)
    POJ 1258 Agri-Net(最小生成树,基础)
    Redhat修改语言
    Rdesktop
    CentOS/Redhat VNC 服务
    RHCS配置web高可用集群
  • 原文地址:https://www.cnblogs.com/leischen/p/2560491.html
Copyright © 2011-2022 走看看