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

    慢慢积累,加油

  • 相关阅读:
    胖子哥的大数据之路(12)-三张图告诉你大数据安全方案设计
    Building the Unstructured Data Warehouse: Architecture, Analysis, and Design
    Hadoop专业解决方案-第3章:MapReduce处理数据
    胖子哥的大数据之路(11)-我看Intel&&Cloudera的合作
    胖子哥的大数据之路(10)- 基于Hive构建数据仓库实例
    胖子哥的大数据之路(9)-数据仓库金融行业数据逻辑模型FS-LDM
    胖子哥的大数据之路(8)- 数据仓库命名规范
    胖子哥的大数据之路(7)- 传统企业切入核心or外围
    dos 打开计算机管理
    C# SQLite数据库
  • 原文地址:https://www.cnblogs.com/lc-ant/p/4226191.html
Copyright © 2011-2022 走看看