Func<T1,T2,TResult> 委托

定义

封装一个方法,该方法具有两个参数,并返回由 TResult 参数指定的类型的值。

C#
public delegate TResult Func<in T1,in T2,out TResult>(T1 arg1, T2 arg2);
C#
public delegate TResult Func<T1,T2,TResult>(T1 arg1, T2 arg2);

类型参数

T1

此委托封装的方法的第一个参数的类型。

这是逆变类型参数。 即,可以使用指定的类型,也可以使用派生程度较低的任何类型。 有关协变和逆变的详细信息,请参阅泛型中的协变和逆变
T2

此委托封装的方法的第二个参数的类型。

这是逆变类型参数。 即,可以使用指定的类型,也可以使用派生程度较低的任何类型。 有关协变和逆变的详细信息,请参阅泛型中的协变和逆变
TResult

此委托封装的方法的返回值类型。

这是协变类型参数。 即,可以使用指定的类型,也可以使用派生程度较高的任何类型。 有关协变和逆变的详细信息,请参阅泛型中的协变和逆变

参数

arg1
T1

此委托封装的方法的第一个参数。

arg2
T2

此委托封装的方法的第二个参数。

返回值

TResult

此委托封装的方法的返回值。

示例

以下示例演示如何声明和使用 Func<T1,T2,TResult> 委托。 此示例声明一个变量,并为其分配一个 Func<T1,T2,TResult> lambda 表达式,该表达式采用值 StringInt32 值作为参数。 如果参数的String长度等于参数的值Int32,则 lambda 表达式将true返回。 封装此方法的委托随后在查询中用于筛选字符串数组中的字符串。

C#
using System;
using System.Collections.Generic;
using System.Linq;

public class Func3Example
{
   public static void Main()
   {
      Func<String, int, bool> predicate = (str, index) => str.Length == index;

      String[] words = { "orange", "apple", "Article", "elephant", "star", "and" };
      IEnumerable<String> aWords = words.Where(predicate).Select(str => str);

      foreach (String word in aWords)
         Console.WriteLine(word);
   }
}

注解

可以使用此委托来表示可以作为参数传递的方法,而无需显式声明自定义委托。 封装的方法必须与此委托定义的方法签名相对应。 这意味着封装的方法必须有两个参数,每个参数都按值传递给它,并且必须返回一个值。

备注

若要引用具有两个参数的方法,并在 F#) (或 Visual Basic 中返回void (unit,该方法声明为FunctionSub) ,请改用泛型Action<T1,T2>委托。

使用 Func<T1,T2,TResult> 委托时,无需显式定义封装具有两个参数的方法的委托。 例如,以下代码显式声明命名 ExtractMethod 的委托,并将对方法的 ExtractWords 引用分配给其委托实例。

C#
using System;

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

public class DelegateExample
{
   public static void Main()
   {
      // Instantiate delegate to reference ExtractWords method
      ExtractMethod extractMeth = ExtractWords;
      string title = "The Scarlet Letter";
      // Use delegate instance to call ExtractWords method and display result
      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> 委托而不是显式定义新委托并向其分配命名方法来简化此代码。

C#
using System;

public class GenericFunc
{
   public static void Main()
   {
      // Instantiate delegate to reference ExtractWords method
      Func<string, int, string[]> extractMethod = ExtractWords;
      string title = "The Scarlet Letter";
      // Use delegate instance to call ExtractWords method and display result
      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);
   }
}

可以将委托与 C# 中的匿名方法配合使用 Func<T1,T2,TResult> ,如以下示例所示。 (有关匿名方法简介,请参阅 Anonymous Methods.)

C#
using System;

public class Anonymous
{
   public static void Main()
   {
      Func<string, int, string[]> extractMeth = delegate(string s, int i)
         { char[] delimiters = new char[] {' '};
           return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
         };

      string title = "The Scarlet Letter";
      // Use Func instance to call ExtractWords method and display result
      foreach (string word in extractMeth(title, 5))
         Console.WriteLine(word);
   }
}

还可以将 lambda 表达式分配给 Func<T1,T2,TResult> 委托,如以下示例所示。 (有关 lambda 表达式简介,请参阅 Lambda 表达式 (VB) Lambda 表达式 (C#) lambda 表达式 (F#) .)

C#
using System;

public class LambdaExpression
{
   public static void Main()
   {
      char[] separators = new char[] {' '};
      Func<string, int, string[]> extract = (s, i) =>
           i > 0 ? s.Split(separators, i) : s.Split(separators) ;

      string title = "The Scarlet Letter";
      // Use Func instance to call ExtractWords method and display result
      foreach (string word in extract(title, 5))
         Console.WriteLine(word);
   }
}

lambda 表达式的基础类型是泛型 Func 委托之一。 这样就可以将 lambda 表达式作为参数传递,而无需将其显式分配给委托。 具体而言,由于命名空间中的 System.Linq 许多类型方法都有 Func<T1,T2,TResult> 参数,因此可以传递这些方法,而无需显式实例化 Func<T1,T2,TResult> 委托。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅