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

    在使用 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);      
       }
    }
    

    也可以将 Action<T> 委托与匿名方法一起使用。

    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> 委托实例。

    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);      
       }
    }

    下面使用 Action<T> 委托来打印 List<T> 对象的内容。 使用 Print 方法将列表的内容显示到控制台上。 此外还使用匿名方法将内容显示到控制台上。 请注意该示例不显式声明 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
     */
    
  • 相关阅读:
    黄聪:Visual Studio快速封装字段方法
    黄聪:在vs2008中设置jquery智能提示
    黄聪:Linq初级班 Linq to DataSet体验(单表、多表联合查询JOIN语法)
    mysql分区
    为什么使用框架
    阅读杂记(RSA,PDO)
    Golang之继承模拟
    php中$_REQUEST一个注意点
    记录mysql性能查询过程
    知识杂记
  • 原文地址:https://www.cnblogs.com/luoht/p/1923882.html
Copyright © 2011-2022 走看看