C#--Action<T>和Func<T>的用法

C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托是保存对某个方法引用的一种引用类型变量

使用Action<T1, T2>委托,则不需要显式定义委托,用于封装具有两个参数的方法。

若要引用的方法,具有两个参数并返回一个值,使用泛型Func<T1, T2, TResult>委托。

一、使用delegate定义没有返回值的委托,C#代码实现:

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}\n{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}\n{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}\n{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}\n{1}", string1, string2);
      }
      catch
      {
         Console.WriteLine("File write operation failed...");
      }
      finally
      {
         if (writer != null) writer.Close();
      }      
   }
}

二、使用delegate定义有返回值的委托:

using System;

delegate string[] ExtractMethod(string stringToManipulate, int maximum);

public class DelegateExample
{
   public static void Main()
   {
      ExtractMethod extractMeth = ExtractWords;
      string title = "The Scarlet Letter";
      foreach (string word in extractMeth(title, 5))
         Console.WriteLine(word);
   }

   private static string[] ExtractWords(string phrase, int limit)
   {
      char[] delimiters = new char[] {' '};
      if (limit > 0)
         return phrase.Split(delimiters, limit);
      else
         return phrase.Split(delimiters);
   }
}

使用泛型Func<T1, T2, TResult>委托, 则不需要显式定义委托:

using System;

public class GenericFunc
{
   public static void Main()
   {
      Func<string, int, string[]> extractMethod = ExtractWords;
      string title = "The Scarlet Letter";
      foreach (string word in extractMethod(title, 5))
         Console.WriteLine(word);
   }

   private static string[] ExtractWords(string phrase, int limit)
   {
      char[] delimiters = new char[] {' '};
      if (limit > 0)
         return phrase.Split(delimiters, limit);
      else
         return phrase.Split(delimiters);
   }
}

总结:

使用Action<T1, T2>委托和Func<T1, T2, TResult>委托,可以简化代码,让逻辑更清晰。

猜你喜欢

转载自blog.csdn.net/qq826364410/article/details/80351333