zoukankan      html  css  js  c++  java
  • .NET中的委托——摘自MSDN

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

    命名空间:  System
    程序集:  mscorlib(在 mscorlib.dll 中)

    语法
     
     
    public delegate void Action<in T>(
    	T obj
    )
    
    

    类型参数

    in T

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

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

    参数

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

    可以使用 Action<T> 委托以参数形式传递方法,而不用显式声明自定义的委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void。 在 Visual Basic 中,必须通过 Sub…End 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
     */
    
  • 相关阅读:
    OpenERP Framework API存档
    OpenERP 7 picking order 继承需要注意的地方
    Unity战斗系统之AI自主决策
    简易2D横版RPG游戏制作
    UGUI之Canvas Group
    UGUI之Canvas和EventSystem
    NGUI之scroll view的制作和踩坑总结
    NGUI之Toggle实现单选框
    Unity中对象池的使用
    继承MonoBehaviour类的优缺点和相关报错
  • 原文地址:https://www.cnblogs.com/JacobQiao/p/4687826.html
Copyright © 2011-2022 走看看