zoukankan      html  css  js  c++  java
  • Func 委托 和 Action 委托 初步谈论

    继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托.

    msdn对于两者的解释:

    Func<TResult>:封装一个不具有参数但却返回 TResult 参数指定的类型值的方法。

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

    两者的区别在于:有无返回值。

    至于 Func<T,TResult>、Func<T1,T2,TResult>....

           Action<T>、Action<T1, T2>.... ,就是参数个数的区别了 。

    借用msdn上面的例子来说明两者的使用方式:

    Func<TResult>:

    using System;
    using System.IO;
    
    public class TestDelegate
    {
       public static void Main()
       {
          OutputTarget output = new OutputTarget();
          Func<bool> methodCall = output.SendToFile;
          if (methodCall())
             Console.WriteLine("Success!"); 
          else
             Console.WriteLine("File write operation failed.");
       }
    }
    
    public class OutputTarget
    {
       public bool SendToFile()
       {
          try
          {
             string fn = Path.GetTempFileName();
             StreamWriter sw = new StreamWriter(fn);
             sw.WriteLine("Hello, World!");
             sw.Close();
             return true;
          }  
          catch
          {
             return false;
          }
       }
    }

    您可以按照以下示例所演示的那样在 C# 中将 Func<TResult> 委托与匿名方法一起使用。

    using System;
    using System.IO;
    
    public class Anonymous
    {
       public static void Main()
       {
          OutputTarget output = new OutputTarget();
          Func<bool> methodCall = delegate() { return output.SendToFile(); };
          if (methodCall())
             Console.WriteLine("Success!"); 
          else
             Console.WriteLine("File write operation failed.");
       }
    }
    
    public class OutputTarget
    {
       public bool SendToFile()
       {
          try
          {
             string fn = Path.GetTempFileName();
             StreamWriter sw = new StreamWriter(fn);
             sw.WriteLine("Hello, World!");
             sw.Close();
             return true;
          }  
          catch
          {
             return false;
          }
       }
    }
    

      您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Func<T, TResult> 委托。

    using System;
    using System.IO;
    
    public class Anonymous
    {
       public static void Main()
       {
          OutputTarget output = new OutputTarget();
          Func<bool> methodCall = () => output.SendToFile(); 
          if (methodCall())
             Console.WriteLine("Success!"); 
          else
             Console.WriteLine("File write operation failed.");
       }
    }
    
    public class OutputTarget
    {
       public bool SendToFile()
       {
          try
          {
             string fn = Path.GetTempFileName();
             StreamWriter sw = new StreamWriter(fn);
             sw.WriteLine("Hello, World!");
             sw.Close();
             return true;
          }  
          catch
          {
             return false;
          }
       }
    }
    

      

    再来看 Action<> 的使用:

    using System;
    using System.Windows.Forms;
    
    public class Name
    {
       private string instanceName;
    
       public Name(string name)
       {
          this.instanceName = name;
       }
    
       public void DisplayToConsole()
       {
          Console.WriteLine(this.instanceName);
       }
    
       public void DisplayToWindow()
       {
          MessageBox.Show(this.instanceName);
       }
    }
    
    public class testTestDelegate
    {
       public static void Main()
       {
          Name testName = new Name("Koani");
          Action showMethod = testName.DisplayToWindow;
          showMethod();
       }
    }

    您也可以按照以下示例所演示的那样在 C# 中将 Action 委托与匿名方法一起使用。

    using System;
    using System.Windows.Forms;
    
    public class Name
    {
       private string instanceName;
    
       public Name(string name)
       {
          this.instanceName = name;
       }
    
       public void DisplayToConsole()
       {
          Console.WriteLine(this.instanceName);
       }
    
       public void DisplayToWindow()
       {
          MessageBox.Show(this.instanceName);
       }
    }
    
    public class Anonymous
    {
       public static void Main()
       {
          Name testName = new Name("Koani");
          Action showMethod = delegate() { testName.DisplayToWindow();} ;
          showMethod();
       }
    }

    您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Action 委托实例。

    using System;
    using System.Windows.Forms;
    
    public class Name
    {
       private string instanceName;
    
       public Name(string name)
       {
          this.instanceName = name;
       }
    
       public void DisplayToConsole()
       {
          Console.WriteLine(this.instanceName);
       }
    
       public void DisplayToWindow()
       {
          MessageBox.Show(this.instanceName);
       }
    }
    
    public class LambdaExpression
    {
       public static void Main()
       {
          Name testName = new Name("Koani");
          Action showMethod = () => testName.DisplayToWindow();
          showMethod();
       }
    }

    慢慢积累,加油

  • 相关阅读:
    .net URL加密和解密
    全面分析VB.NET文件传送
    如何美化你的.net 应用程序 (皮肤使用)
    获取机器的硬件信息(CPU ID序列号, 主板信息,硬盘序列号,系统信息)
    这是博客园的一个Bug吗?
    [转]深入理解JavaScript闭包(closure)
    【翻译】在ASP.NET中的DatePicker控件
    [翻译]ASP.NET MVC 2 Preview 1 发布了
    页面性能优化减少HTTP请求
    [翻译]C#闭包内幕(Looking Inside C# Closures)
  • 原文地址:https://www.cnblogs.com/lc-ant/p/4226191.html
Copyright © 2011-2022 走看看