• C# delegate Action<T> lambda表达式


              转载以记录:http://blog.csdn.net/educast/article/details/7219854

             在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的方法的委托。以下代码显式声明了一个名为 DisplayMessage 的委托,并将对 WriteLine 方法或 ShowWindowsMessage 方法的引用分配给其委托实例。

     1 usingSystem;
     2 usingSystem.Windows.Forms; 
     3 delegatevoid DisplayMessage(stringmessage); 
     4 publicclass TestCustomDelegate{
     5    public static void Main()
     6    {
     7       DisplayMessage messageTarget;  
     8       if(Environment.GetCommandLineArgs().Length > 1)
     9          messageTarget = ShowWindowsMessage;
    10       else
    11          messageTarget = Console.WriteLine; 
    12       messageTarget("Hello, World!");  
    13    }     
    14  
    15    private static void ShowWindowsMessage(stringmessage)
    16    {
    17       MessageBox.Show(message);     
    18    }
    19 }
    20  
    View Code

          以下简化了此代码,它所用的方法是实例化 Action<T> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

     1 usingSystem;
     2 usingSystem.Windows.Forms; 
     3 publicclass TestAction1
     4 {
     5    publicstatic void Main()
     6    {
     7       Action<string> messageTarget; 
     8       if(Environment.GetCommandLineArgs().Length > 1)
     9          messageTarget = ShowWindowsMessage;
    10       else
    11          messageTarget = Console.WriteLine; 
    12       messageTarget("Hello, World!");  
    13    }     
    14  
    15    privatestatic void ShowWindowsMessage(stringmessage)
    16    {
    17       MessageBox.Show(message);     
    18    }
    19 }
    View Code

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

     1 usingSystem;
     2 usingSystem.Windows.Forms;
     3  
     4 publicclass TestAnonMethod
     5 {
     6    publicstatic void Main()
     7    {
     8       Action<string> messageTarget;  
     9       if(Environment.GetCommandLineArgs().Length > 1)
    10          messageTarget = delegate(strings) { ShowWindowsMessage(s); };
    11       else
    12          messageTarget = delegate(strings) { Console.WriteLine(s); }; 
    13       messageTarget("Hello, World!");
    14    }
    15  
    16    privatestatic void ShowWindowsMessage(stringmessage)
    17    {
    18       MessageBox.Show(message);     
    19    }
    20 }
    View Code

         也可以将 lambda 表达式分配给 Action<T> 委托实例。

     1 usingSystem;
     2 usingSystem.Windows.Forms;
     3  
     4 public class TestLambdaExpression
     5 {
     6    public static void Main()
     7    {
     8       Action<string> messageTarget; 
     9  
    10       if(Environment.GetCommandLineArgs().Length > 1)
    11          messageTarget = s => ShowWindowsMessage(s); 
    12       else
    13          messageTarget = s => Console.WriteLine(s);
    14  
    15       messageTarget("Hello, World!");
    16    }
    17  
    18    private static void ShowWindowsMessage(stringmessage)
    19    {
    20       MessageBox.Show(message);     
    21    }
    22 }
    View Code

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

     1 usingSystem;
     2 usingSystem.Collections.Generic;
     3  
     4 classProgram
     5 {
     6     staticvoid Main()
     7     {
     8         List<String> names = newList<String>();
     9         names.Add("Bruce");
    10         names.Add("Alfred");
    11         names.Add("Tim");
    12         names.Add("Richard");
    13  
    14         // Display the contents of the list using the Print method.
    15         names.ForEach(Print);
    16  
    17         // The following demonstrates the anonymous method feature of C#
    18         // to display the contents of the list to the console.
    19         names.ForEach(delegate(String name)
    20         {
    21             Console.WriteLine(name);
    22         });
    23     }
    24  
    25     privatestatic void Print(strings)
    26     {
    27         Console.WriteLine(s);
    28     }
    29 }
    30 /* This code will produce output similar to the following:
    31  * Bruce
    32  * Alfred
    33  * Tim
    34  * Richard
    35  * Bruce
    36  * Alfred
    37  * Tim
    38  * Richard
    39  */
    View Code
  • 相关阅读:
    aaa
    记一次Vue实战总结
    Data too long for column 'xxx' at row 1MySql.Data.MySqlClient.MySqlPacket ReadPacket() 报错解决
    uni-app 监听返回按钮
    微信H5分享外部链接,缩略图不显示
    uni-app 动态控制下拉刷新
    vueX 的使用
    uni-app H5 腾讯地图无法导航
    uni-app支付功能
    hooks 与 animejs
  • 原文地址:https://www.cnblogs.com/kanite/p/6526651.html
Copyright © 2020-2023  润新知