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

    慢慢积累,加油

  • 相关阅读:
    Test
    Python Requests库使用指南
    Python文件操作,看这篇就足够
    Ubuntu配置完全教程
    Redis入门
    Python Requests 库学习笔记
    c++11函数模板“偏特化”的一种实现
    c++通用判零模板类
    Python实现1-9数组形成的结果为100的所有运算式
    QT:用QWebSocket实现webchannel,实现C++与HTML通信
  • 原文地址:https://www.cnblogs.com/lc-ant/p/4226191.html
Copyright © 2011-2022 走看看