• Action<T1, T2>委托


    封装包含两个参数的方法委托,没有返回值。

    语法

    public delegate void Action<in T1, in T2>(
        T1 arg1,
        T2 arg2
    )

    类型参数

    in T1:委托封装方法的第一个参数类型,此类型参数逆变。

    用法

    可以使用Action<T1, T2>委托以参数形式传递方法,而不用自定义委托。封装的方法必须与此委托的方法签名一致。也就是说,封装的方法也要有两个参数,没有返回值。

    下面显式声明了一个名为ConcatStrings的委托。然后,它将两个方法中的任意一个的引用分配给其委托实例。其中一个方法将两个字符串写入控制台;另一个将两个字符串写入文件。

    using System;
    using System.IO;
    
    delegate void ConcatStrings(string string1, string string2);
    
    public class TestDelegate
    {
       public static void Main()
       {
          string message1 = "The first line of a message.";
          string message2 = "The second line of a message.";
          ConcatStrings concat;
    
          if (Environment.GetCommandLineArgs().Length > 1)
             concat = WriteToFile;
          else
             concat = WriteToConsole;
    
          concat(message1, message2);
       }
    
       private static void WriteToConsole(string string1, string string2)
       {
          Console.WriteLine("{0}
    {1}", string1, string2);            
       }
    
       private static void WriteToFile(string string1, string string2)
       {
          StreamWriter writer = null;  
          try
          {
             writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
             writer.WriteLine("{0}
    {1}", string1, string2);
          }
          catch
          {
             Console.WriteLine("File write operation failed...");
          }
          finally
          {
             if (writer != null) writer.Close();
          }      
       }
    }

    下面以Action<T1, T2>委托简化上面的代码:

    using System;
    using System.IO;
    
    public class TestAction2
    {
       public static void Main()
       {
          string message1 = "The first line of a message.";
          string message2 = "The second line of a message.";
          Action<string, string> concat;
    
          if (Environment.GetCommandLineArgs().Length > 1)
             concat = WriteToFile;
          else
             concat = WriteToConsole;
    
          concat(message1, message2);
       }
    
       private static void WriteToConsole(string string1, string string2)
       {
          Console.WriteLine("{0}
    {1}", string1, string2);            
       }
    
       private static void WriteToFile(string string1, string string2)
       {
          StreamWriter writer = null;  
          try
          {
             writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
             writer.WriteLine("{0}
    {1}", string1, string2);
          }
          catch
          {
             Console.WriteLine("File write operation failed...");
          }
          finally
          {
             if (writer != null) writer.Close();
          }      
       }
    }

    其实就是预先定义好的委托,不需要自定义对应参数的委托了。

    还可以同匿名方法一起使用:

    using System;
    using System.IO;
    
    public class TestAnonymousMethod
    {
       public static void Main()
       {
          string message1 = "The first line of a message.";
          string message2 = "The second line of a message.";
          Action<string, string> concat;
    
          if (Environment.GetCommandLineArgs().Length > 1)
             concat = delegate(string s1, string s2) { WriteToFile(s1, s2); };
          else
             concat = delegate(string s1, string s2) { WriteToConsole(s1, s2);} ;
    
          concat(message1, message2);
       }
    
       private static void WriteToConsole(string string1, string string2)
       {
          Console.WriteLine("{0}
    {1}", string1, string2);            
       }
    
       private static void WriteToFile(string string1, string string2)
       {
          StreamWriter writer = null;  
          try
          {
             writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
             writer.WriteLine("{0}
    {1}", string1, string2);
          }
          catch
          {
             Console.WriteLine("File write operation failed...");
          }
          finally
          {
             if (writer != null) writer.Close();
          }      
       }
    }

    也可以将匿名函数替换为lambda表达式:

    using System;
    using System.IO;
    
    public class TestLambdaExpression
    {
       public static void Main()
       {
          string message1 = "The first line of a message.";
          string message2 = "The second line of a message.";
          Action<string, string> concat;
    
          if (Environment.GetCommandLineArgs().Length > 1)
             concat = (s1, s2) => WriteToFile(s1, s2);
          else
             concat = (s1, s2) => WriteToConsole(s1, s2);
    
          concat(message1, message2);
       }
    
       private static void WriteToConsole(string string1, string string2)
       {
          Console.WriteLine("{0}
    {1}", string1, string2);            
       }
    
       private static void WriteToFile(string string1, string string2)
       {
          StreamWriter writer = null;  
          try
          {
             writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
             writer.WriteLine("{0}
    {1}", string1, string2);
          }
          catch
          {
             Console.WriteLine("File write operation failed...");
          }
          finally
          {
             if (writer != null) writer.Close();
          }      
       }
    }

    后两者都没有起到简化代码的作用。

  • 相关阅读:
    极客标签编程小挑战#31:生成注册页面的显示效果
    极客Web前端开发资源大荟萃#017
    知道你们不想撸代码写PPT之可视化页面做一款炫酷的WEB PPT
    使用jQuery图表插件Sparklines来开发一个实用的网站PV(page view)实时监控应用
    javascript专业八级测试答案整理
    使用Raphaël类库实现的超酷动画技能图表
    极客编程小挑战#26:实现日期级联下拉选择框
    gulp初印象
    程序语言,编译?解释?
    发了这嘛多技术文章,今天给大家点福利吧!邻家小美女一枚,想在北京找个工作,大家来看看给出点主意。
  • 原文地址:https://www.cnblogs.com/jiawei-whu/p/4341316.html
Copyright © 2020-2023  润新知