使用英语阅读

通过


Func<T1,T2,T3,T4,TResult> 委托

定义

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

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

类型参数

T1

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

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

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

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

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

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

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

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

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

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

参数

arg1
T1

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

arg2
T2

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

arg3
T3

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

arg4
T4

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

返回值

TResult

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

示例

以下示例演示如何声明和使用 Func<T1,T2,TResult> 委托。 此示例声明一个 Func<T1,T2,TResult> 变量,并为其分配一个 lambda 表达式,该表达式将 String 值和 Int32 值作为参数。 如果参数的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 中声明为 而不是) unit 的四个SubFunction 参数并返回void (的方法,请改用泛型Action<T1,T2,T3,T4>委托。

使用 Func<T1,T2,T3,T4,TResult> 委托时,无需显式定义一个委托,该委托封装了具有四个参数的方法。 例如,以下代码显式声明名为 的 Searcher 泛型委托,并将对 方法的 IndexOf 引用分配给其委托实例。

C#
using System;

delegate int Searcher(string searchString, int start, int count,
                         StringComparison type);

public class DelegateExample
{
   public static void Main()
   {
      string title = "The House of the Seven Gables";
      int position = 0;
      Searcher finder = title.IndexOf;
      do
      {
         int characters = title.Length - position;
         position = finder("the", position, characters,
                         StringComparison.InvariantCultureIgnoreCase);
         if (position >= 0)
         {
            position++;
            Console.WriteLine("'The' found at position {0} in {1}.",
                              position, title);
         }
      } while (position > 0);
   }
}

以下示例通过实例化 Func<T1,T2,T3,T4,TResult> 委托而不是显式定义新委托并向其分配命名方法来简化此代码。

C#
using System;

public class DelegateExample
{
   public static void Main()
   {
      string title = "The House of the Seven Gables";
      int position = 0;
      Func<string, int, int, StringComparison, int> finder = title.IndexOf;
      do
      {
         int characters = title.Length - position;
         position = finder("the", position, characters,
                         StringComparison.InvariantCultureIgnoreCase);
         if (position >= 0)
         {
            position++;
            Console.WriteLine("'The' found at position {0} in {1}.",
                              position, title);
         }
      } while (position > 0);
   }
}

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

C#
using System;

public class DelegateExample
{
   public static void Main()
   {
      string title = "The House of the Seven Gables";
      int position = 0;
      Func<string, int, int, StringComparison, int> finder =
           delegate(string s, int pos, int chars, StringComparison type)
           { return title.IndexOf(s, pos, chars, type); };
      do
      {
         int characters = title.Length - position;
         position = finder("the", position, characters,
                         StringComparison.InvariantCultureIgnoreCase);
         if (position >= 0)
         {
            position++;
            Console.WriteLine("'The' found at position {0} in {1}.",
                              position, title);
         }
      } while (position > 0);
   }
}

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

C#
using System;

public class DelegateExample
{
   public static void Main()
   {
      string title = "The House of the Seven Gables";
      int position = 0;
      Func<string, int, int, StringComparison, int> finder =
           (s, pos, chars, type) => title.IndexOf(s, pos, chars, type);
      do
      {
         int characters = title.Length - position;
         position = finder("the", position, characters,
                         StringComparison.InvariantCultureIgnoreCase);
         if (position >= 0)
         {
            position++;
            Console.WriteLine("'The' found at position {0} in {1}.",
                              position, title);
         }
      } while (position > 0);
   }
}

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

扩展方法

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, 8, 9
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅