Func<T1,T2,T3,T4,TResult> 代理人

定義

4 つのパラメーターを持ち、 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<in T1,in T2,in T3,in T4,out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4) where T1 : allows ref struct where T2 : allows ref struct where T3 : allows ref struct where T4 : allows ref struct where TResult : allows ref struct;
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

このデリゲートがカプセル化するメソッドの 2 番目のパラメーターの型。

この型パラメーターは反変です。 つまり、指定した型、または弱い派生型のいずれかを使用することができます。 共変性および反変性の詳細については、「ジェネリックの共変性と反変性」をご覧ください。
T3

このデリゲートがカプセル化するメソッドの 3 番目のパラメーターの型。

この型パラメーターは反変です。 つまり、指定した型、または弱い派生型のいずれかを使用することができます。 共変性および反変性の詳細については、「ジェネリックの共変性と反変性」をご覧ください。
T4

このデリゲートがカプセル化するメソッドの 4 番目のパラメーターの型。

この型パラメーターは反変です。 つまり、指定した型、または弱い派生型のいずれかを使用することができます。 共変性および反変性の詳細については、「ジェネリックの共変性と反変性」をご覧ください。
TResult

このデリゲートがカプセル化するメソッドの戻り値の型。

この型パラメーターは共変です。 つまり、指定した型、または強い派生型のいずれかを使用することができます。 共変性および反変性の詳細については、「ジェネリックの共変性と反変性」をご覧ください。

パラメーター

arg1
T1

このデリゲートがカプセル化するメソッドの最初のパラメーター。

arg2
T2

このデリゲートがカプセル化するメソッドの 2 番目のパラメーター。

arg3
T3

このデリゲートがカプセル化するメソッドの 3 番目のパラメーター。

arg4
T4

このデリゲートがカプセル化するメソッドの 4 番目のパラメーター。

戻り値

TResult

このデリゲートがカプセル化するメソッドの戻り値。

次の例では、 Func<T1,T2,TResult> デリゲートを宣言して使用する方法を示します。 この例では、 Func<T1,T2,TResult> 変数を宣言し、 String 値と Int32 値をパラメーターとして受け取るラムダ式を割り当てます。 ラムダ式は、String パラメーターの長さが Int32 パラメーターの値と等しい場合、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

注釈

このデリゲートを使用すると、カスタム デリゲートを明示的に宣言せずにパラメーターとして渡すことができるメソッドを表すことができます。 カプセル化されたメソッドは、このデリゲートによって定義されているメソッド シグネチャに対応している必要があります。 つまり、カプセル化されたメソッドには 4 つのパラメーターが必要であり、それぞれが値によって渡され、値を返す必要があります。

4 つのパラメーターを持ち、void (F# では unit) を返すメソッド (または、Function としてではなく、Sub として宣言されているVisual Basic) を参照するには、代わりにジェネリック Action<T1,T2,T3,T4> デリゲートを使用します。

Func<T1,T2,T3,T4,TResult> デリゲートを使用する場合、4 つのパラメーターを持つメソッドをカプセル化するデリゲートを明示的に定義する必要はありません。 たとえば、次のコードは、 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

次の例に示すように、 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 =
           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);
   }
}

次の例に示すように、ラムダ式を Func<T1,T2,TResult> デリゲートに割り当てることもできます。 (ラムダ式の概要については、「 ラムダ式 (VB)ラムダ式 (C#)ラムダ式 (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

ラムダ式の基になる型は、ジェネリック Func デリゲートの 1 つです。 これにより、デリゲートに明示的に割り当てることなく、ラムダ式をパラメーターとして渡すことができます。 特に、 System.Linq 名前空間の型の多くのメソッドには Func パラメーターがあるため、 Func デリゲートを明示的にインスタンス化することなく、これらのメソッドにラムダ式を渡すことができます。

拡張メソッド

名前 説明
GetMethodInfo(Delegate)

指定したデリゲートによって表されるメソッドを表すオブジェクトを取得します。

適用対象

こちらもご覧ください