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

定义

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

generic <typename T1, typename T2, typename T3, typename T4, typename TResult>
public delegate TResult Func(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<in T1,in T2,in T3,in T4,out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<T1,T2,T3,T4,TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
type Func<'T1, 'T2, 'T3, 'T4, 'Result> = delegate of 'T1 * 'T2 * 'T3 * 'T4 -> 'Result
Public Delegate Function Func(Of In T1, In T2, In T3, In T4, Out TResult)(arg1 As T1, arg2 As T2, arg3 As T3, arg4 As T4) As TResult 
Public Delegate Function Func(Of T1, T2, T3, T4, TResult)(arg1 As T1, arg2 As T2, arg3 As T3, arg4 As T4) As TResult 

类型参数

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 。 封装此方法的委托随后在查询中使用,以筛选字符串数组中的字符串。

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);
   }
}
open System
open System.Linq

let predicate = Func<string, int, bool>(fun str index -> str.Length = index)

let words = [ "orange"; "apple"; "Article"; "elephant"; "star"; "and" ]
let aWords = words.Where predicate

for word in aWords do
    printfn $"{word}"
Imports System.Collections.Generic
Imports System.Linq

Public Module Func3Example

   Public Sub Main()
      Dim predicate As Func(Of String, Integer, Boolean) = Function(str, index) str.Length = index

      Dim words() As String = { "orange", "apple", "Article", "elephant", "star", "and" }
      Dim aWords As IEnumerable(Of String) = words.Where(predicate)

      For Each word As String In aWords
         Console.WriteLine(word)
      Next   
   End Sub
End Module

注解

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

注意

若要引用在 F#) (或 Visual Basic 中声明为 而不是) unit 的四个SubFunction 参数并返回void (的方法,请改用泛型Action<T1,T2,T3,T4>委托。

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

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);
   }
}
open System

type Searcher = delegate of (string * int * int * StringComparison) -> int

let title = "The House of the Seven Gables"
let finder = Searcher title.IndexOf
let mutable position = 0

while position > -1 do
    let characters = title.Length - position
    position <- 
        finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
    if position >= 0 then
        position <- position + 1
        printfn $"'The' found at position {position} in {title}."
Delegate Function Searcher(searchString As String, _
                           start As Integer,  _
                           count As Integer, _
                           type As StringComparison) As Integer

Module DelegateExample
   Public Sub Main()
      Dim title As String = "The House of the Seven Gables"
      Dim position As Integer = 0
      Dim finder As Searcher = AddressOf title.IndexOf
      Do
         Dim characters As Integer = title.Length - position
         position = finder("the", position, characters, _
                         StringComparison.InvariantCultureIgnoreCase) 
         If position >= 0 Then
            position += 1
            Console.WriteLine("'The' found at position {0} in {1}.", _
                              position, title)
         End If   
      Loop While position > 0   
   End Sub
End Module

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

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);
   }
}
open System

let indexOf (s: string) s2 pos chars comparison =
    s.IndexOf(s2, pos, chars, comparison) 

let title = "The House of the Seven Gables"
let finder = Func<string, int, int, StringComparison, int>(indexOf title)
let mutable position = 0

while position > -1 do
    let characters = title.Length - position
    position <- 
        finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
    if position >= 0 then
        position <- position + 1
        printfn $"'The' found at position {position} in {title}."
Module DelegateExample
   Public Sub Main()
      Dim title As String = "The House of the Seven Gables"
      Dim position As Integer = 0
      Dim finder As Func(Of String, Integer, Integer, StringComparison, Integer) _
                    = AddressOf title.IndexOf
      Do
         Dim characters As Integer = title.Length - position
         position = finder("the", position, characters, _
                         StringComparison.InvariantCultureIgnoreCase) 
         If position >= 0 Then
            position += 1
            Console.WriteLine("'The' found at position {0} in {1}.", _
                              position, title)
         End If   
      Loop While position > 0   
   End Sub
End Module

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

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#) .)

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);
   }
}
open System

let title = "The House of the Seven Gables"

let finder =
    Func<string, int, int, StringComparison, int>(fun s pos chars typ -> title.IndexOf(s, pos, chars, typ))

let mutable position = 0

while position > -1 do
    let characters = title.Length - position
    position <- finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)

    if position >= 0 then
        position <- position + 1
        printfn $"'The' found at position {position} in {title}."
Module DelegateExample
   Public Sub Main()
      Dim title As String = "The House of the Seven Gables"
      Dim position As Integer = 0
      Dim finder As Func(Of String, Integer, Integer, StringComparison, Integer) _
                    = Function(s, pos, chars, type) _
                    title.IndexOf(s, pos, chars, type)
      Do
         Dim characters As Integer = title.Length - position
         position = finder("the", position, characters, _
                         StringComparison.InvariantCultureIgnoreCase) 
         If position >= 0 Then
            position += 1
            Console.WriteLine("'The' found at position {0} in {1}.", _
                              position, title)
         End If   
      Loop While position > 0   
   End Sub
End Module

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

扩展方法

GetMethodInfo(Delegate)

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

适用于

另请参阅