Func<T1,T2,T3,T4,TResult> Delegat

Definicja

Hermetyzuje metodę, która ma cztery parametry i zwraca wartość typu określonego TResult przez parametr .

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 

Parametry typu

T1

Typ pierwszego parametru metody, który ten delegat hermetyzuje.

Ten parametr typu jest kontrawariantny. Oznacza to, że możesz użyć typu, który został przez Ciebie określony, lub dowolnego typu, który jest mniej pochodny. Aby uzyskać więcej informacji o kowariancji i kontrawariancji, zobacz Kowariancja i kontrawariancja w typach ogólnych.
T2

Typ drugiego parametru metody, który ten delegat hermetyzuje.

Ten parametr typu jest kontrawariantny. Oznacza to, że możesz użyć typu, który został przez Ciebie określony, lub dowolnego typu, który jest mniej pochodny. Aby uzyskać więcej informacji o kowariancji i kontrawariancji, zobacz Kowariancja i kontrawariancja w typach ogólnych.
T3

Typ trzeciego parametru metody, który ten delegat hermetyzuje.

Ten parametr typu jest kontrawariantny. Oznacza to, że możesz użyć typu, który został przez Ciebie określony, lub dowolnego typu, który jest mniej pochodny. Aby uzyskać więcej informacji o kowariancji i kontrawariancji, zobacz Kowariancja i kontrawariancja w typach ogólnych.
T4

Typ czwartego parametru metody, który ten delegat hermetyzuje.

Ten parametr typu jest kontrawariantny. Oznacza to, że możesz użyć typu, który został przez Ciebie określony, lub dowolnego typu, który jest mniej pochodny. Aby uzyskać więcej informacji o kowariancji i kontrawariancji, zobacz Kowariancja i kontrawariancja w typach ogólnych.
TResult

Typ zwracanej wartości metody, którą hermetyzuje ten delegat.

Ten parametr typu jest kowariantny. Oznacza to, że możesz użyć typu, który został przez Ciebie określony, lub dowolnego typu, który jest bardziej pochodny. Aby uzyskać więcej informacji o kowariancji i kontrawariancji, zobacz Kowariancja i kontrawariancja w typach ogólnych.

Parametry

arg1
T1

Pierwszy parametr metody, który ten delegat hermetyzuje.

arg2
T2

Drugi parametr metody, który ten delegat hermetyzuje.

arg3
T3

Trzeci parametr metody, którą hermetyzuje ten delegat.

arg4
T4

Czwarty parametr metody, którą hermetyzuje ten delegat.

Wartość zwracana

TResult

Zwracana wartość metody, którą hermetyzuje ten delegat.

Przykłady

W poniższym przykładzie pokazano, jak zadeklarować delegata i używać go Func<T1,T2,TResult> . W tym przykładzie zadeklarowano zmienną Func<T1,T2,TResult> i przypisano do niej wyrażenie lambda, które przyjmuje String wartość i Int32 wartość jako parametry. Wyrażenie lambda zwraca true wartość , jeśli długość String parametru jest równa wartości parametru Int32 . Delegat hermetyzujący tę metodę jest następnie używany w zapytaniu do filtrowania ciągów w tablicy ciągów.

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

Uwagi

Tego delegata można użyć do reprezentowania metody, którą można przekazać jako parametr bez jawnego deklarowania delegata niestandardowego. Hermetyzowana metoda musi odpowiadać podpisowi metody zdefiniowanej przez ten delegat. Oznacza to, że hermetyzowana metoda musi mieć cztery parametry, z których każda jest przekazywana przez wartość, i że musi zwrócić wartość.

Uwaga

Aby odwołać się do metody, która ma cztery parametry i zwraca wartość (unit w języku F#) (lub w Visual Basic, która jest zadeklarowana jako a Function``Sub nie jako ), należy zamiast tego użyć delegata ogólnegoAction<T1,T2,T3,T4>.void

Jeśli używasz delegata Func<T1,T2,T3,T4,TResult> , nie musisz jawnie definiować delegata, który hermetyzuje metodę z czterema parametrami. Na przykład poniższy kod jawnie deklaruje delegata ogólnego o nazwie Searcher i przypisuje odwołanie do metody do IndexOf jej wystąpienia delegata.

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

Poniższy przykład upraszcza ten kod przez utworzenie wystąpienia delegata Func<T1,T2,T3,T4,TResult> zamiast jawnego zdefiniowania nowego delegata i przypisania do niego nazwanej metody.

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

Delegata można używać Func<T1,T2,T3,T4,TResult> z metodami anonimowymi w języku C#, jak pokazano w poniższym przykładzie. (Aby zapoznać się z wprowadzeniem do metod anonimowych, zobacz Metody anonimowe).

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

Można również przypisać wyrażenie lambda do delegata Func<T1,T2,TResult> , jak pokazano w poniższym przykładzie. (Aby zapoznać się z wprowadzeniem do wyrażeń lambda, zobacz Wyrażenia lambda (VB), Lambda Expressions (C#) i Lambda Expressions (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

Podstawowym typem wyrażenia lambda jest jeden z delegatów ogólnych Func . Dzięki temu można przekazać wyrażenie lambda jako parametr bez jawnego przypisania go do delegata. W szczególności, ponieważ wiele metod typów w System.Linq przestrzeni nazw ma Func parametry, można przekazać te metody wyrażenie lambda bez jawnego utworzenia wystąpienia delegata Func .

Metody rozszerzania

GetMethodInfo(Delegate)

Pobiera obiekt reprezentujący metodę reprezentowaną przez określonego delegata.

Dotyczy

Zobacz też