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

慢慢积累,加油

原文地址:https://www.cnblogs.com/lc-ant/p/4226191.html