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

Tanım

Dört parametresi olan bir yöntemi kapsüller ve parametresi tarafından TResult belirtilen türde bir değer döndürür.

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 

Tür Parametreleri

T1

Bu temsilcinin kapsüllediğini yöntemin ilk parametresinin türü.

Bu genel tür parametresi kontravaryanttır. Bu, kendi belirttiğiniz türü veya daha az türetilmiş başka bir türü kullanabileceğiniz anlamına gelir. Kovaryans ve kontravaryans hakkında daha fazla bilgi için bkz. Genel Türlerde Kovaryans ve Kontravaryans.
T2

Bu temsilcinin kapsüllediğini yöntemin ikinci parametresinin türü.

Bu genel tür parametresi kontravaryanttır. Bu, kendi belirttiğiniz türü veya daha az türetilmiş başka bir türü kullanabileceğiniz anlamına gelir. Kovaryans ve kontravaryans hakkında daha fazla bilgi için bkz. Genel Türlerde Kovaryans ve Kontravaryans.
T3

Bu temsilcinin kapsüllediğini yöntemin üçüncü parametresinin türü.

Bu genel tür parametresi kontravaryanttır. Bu, kendi belirttiğiniz türü veya daha az türetilmiş başka bir türü kullanabileceğiniz anlamına gelir. Kovaryans ve kontravaryans hakkında daha fazla bilgi için bkz. Genel Türlerde Kovaryans ve Kontravaryans.
T4

Bu temsilcinin kapsüllediğini yöntemin dördüncü parametresinin türü.

Bu genel tür parametresi kontravaryanttır. Bu, kendi belirttiğiniz türü veya daha az türetilmiş başka bir türü kullanabileceğiniz anlamına gelir. Kovaryans ve kontravaryans hakkında daha fazla bilgi için bkz. Genel Türlerde Kovaryans ve Kontravaryans.
TResult

Bu temsilcinin kapsüllediğini yöntemin dönüş değerinin türü.

Bu genel tür parametresi kovaryanttır. Bu, kendi belirttiğiniz türü veya daha fazla türetilmiş başka bir türü kullanabileceğiniz anlamına gelir. Kovaryans ve kontravaryans hakkında daha fazla bilgi için bkz. Genel Türlerde Kovaryans ve Kontravaryans.

Parametreler

arg1
T1

Bu temsilcinin kapsüllediğini yönteminin ilk parametresi.

arg2
T2

Bu temsilcinin kapsüllediğini yönteminin ikinci parametresi.

arg3
T3

Bu temsilcinin kapsüllediğini yönteminin üçüncü parametresi.

arg4
T4

Bu temsilcinin kapsüllediğini yönteminin dördüncü parametresi.

Dönüş Değeri

TResult

Bu temsilcinin kapsüllediğini yönteminin dönüş değeri.

Örnekler

Aşağıdaki örnekte temsilci bildirme ve kullanma Func<T1,T2,TResult> işlemleri gösterilmektedir. Bu örnekte bir Func<T1,T2,TResult> değişken bildirilip parametre olarak değer ve değer alan bir String Int32 lambda ifadesi atanır. Lambda ifadesi, parametrenin uzunluğu parametrenin String değerine Int32 eşitse döndürürtrue. Bu yöntemi kapsülleyen temsilci daha sonra bir sorguda bir dize dizisindeki dizeleri filtrelemek için kullanılır.

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

Açıklamalar

Özel temsilci açıkça bildirilmeden parametre olarak geçirilebilen bir yöntemi temsil etmek için bu temsilciyi kullanabilirsiniz. Kapsüllenen yöntem, bu temsilci tarafından tanımlanan yöntem imzasına karşılık olmalıdır. Bu, kapsüllenmiş yöntemin her biri değere göre geçirilen ve bir değer döndürmesi gereken dört parametreye sahip olması gerektiği anlamına gelir.

Not

Dört parametresi olan ve döndüren void bir yönteme başvurmak için (unit F#'ta) (veya Visual Basic olarak değil Functionolarak bildirilen), Sub bunun yerine genel Action<T1,T2,T3,T4> temsilciyi kullanın.

Temsilciyi Func<T1,T2,T3,T4,TResult> kullandığınızda, dört parametreli bir yöntemi kapsülleyen bir temsilciyi açıkça tanımlamanız gerekmez. Örneğin, aşağıdaki kod adlı Searcher genel bir temsilciyi açıkça bildirir ve yöntemine temsilci örneğine IndexOf bir başvuru atar.

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

Aşağıdaki örnek, yeni bir temsilciyi açıkça tanımlamak ve buna adlandırılmış bir yöntem atamak yerine temsilcinin Func<T1,T2,T3,T4,TResult> örneğini oluşturarak bu kodu basitleştirir.

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

Aşağıdaki örnekte gösterildiği gibi temsilciyi Func<T1,T2,T3,T4,TResult> C# dilinde anonim yöntemlerle kullanabilirsiniz. (Anonim yöntemlere giriş için bkz . Anonim Yöntemler.)

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

Aşağıdaki örnekte gösterildiği gibi temsilciye lambda Func<T1,T2,TResult> ifadesi de atayabilirsiniz. (Lambda ifadelerine giriş için bkz. Lambda İfadeleri (VB), Lambda İfadeleri (C#) ve Lambda İfadeleri (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 ifadesinin temel türü genel Func temsilcilerden biridir. Bu, bir lambda ifadesini bir temsilciye açıkça atamadan parametre olarak geçirmeyi mümkün kılar. Özellikle, ad alanında System.Linq Func birçok tür yönteminin parametreleri olduğundan, bir temsilciyi açıkça başlatmadan Func bu yöntemleri bir lambda ifadesi geçirebilirsiniz.

Uzantı Metotları

GetMethodInfo(Delegate)

Belirtilen temsilci tarafından temsil edilen yöntemi temsil eden bir nesnesi alır.

Şunlara uygulanır

Ayrıca bkz.