Func<T1,T2,T3,T4,TResult> 대리자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
네 개의 매개 변수가 있는 메서드를 캡슐화하고 매개 변수로 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
이 대리자가 캡슐화하는 메서드의 첫 번째 매개 변수 형식입니다.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- T2
이 대리자가 캡슐화하는 메서드의 두 번째 매개 변수 형식입니다.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- T3
이 대리자가 캡슐화하는 메서드의 세 번째 매개 변수 형식입니다.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- T4
이 대리자가 캡슐화하는 메서드의 네 번째 매개 변수 형식입니다.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- TResult
이 대리자가 캡슐화하는 메서드의 반환 값 형식입니다.
이 형식 매개 변수는 공변(Covariant)입니다. 즉, 지정한 형식이나 더 많게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.매개 변수
- arg1
- T1
이 대리자가 캡슐화하는 메서드의 첫 번째 매개 변수입니다.
- arg2
- T2
이 대리자가 캡슐화하는 메서드의 두 번째 매개 변수입니다.
- arg3
- T3
이 대리자가 캡슐화하는 메서드의 세 번째 매개 변수입니다.
- arg4
- T4
이 대리자가 캡슐화하는 메서드의 네 번째 매개 변수입니다.
반환 값
이 대리자가 캡슐화하는 메서드의 반환 값입니다.
예제
다음 예제에서는 대리자를 선언하고 사용하는 Func<T1,T2,TResult> 방법을 보여 줍니다. 이 예제에서는 변수를 Func<T1,T2,TResult> 선언하고 값과 Int32 값을 매개 변수로 사용하는 String 람다 식을 할당합니다. 매개 변수의 길이가 매개 변수 값 StringInt32 과 같으면 람다 식이 반환 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
설명
이 대리자를 사용하여 사용자 지정 대리자를 명시적으로 선언하지 않고 매개 변수로 전달할 수 있는 메서드를 나타낼 수 있습니다. 캡슐화된 메서드는 이 대리자가 정의한 메서드 서명에 해당해야 합니다. 즉, 캡슐화된 메서드에는 각각 값으로 전달되고 값을 반환해야 하는 네 개의 매개 변수가 있어야 합니다.
메모
네 개의 매개 변수가 있고 void(F#의 경우 unit)(또는 Visual Basic Function 대신 Sub 선언됨)를 반환하는 메서드를 참조하려면 제네릭 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> 수 있습니다. (익명 메서드에 대한 소개는 익명 메서드를 참조하세요.)
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 대리자 중 하나입니다. 이렇게 하면 대리자에게 명시적으로 할당하지 않고 람다 식을 매개 변수로 전달할 수 있습니다. 특히 네임스페이스의 많은 형식 메서드에는 System.LinqFunc 매개 변수가 있으므로 대리자를 명시적으로 인스턴스화하지 않고도 이러한 메서드를 람다 식으로 Func 전달할 수 있습니다.
확장명 메서드
| Name | Description |
|---|---|
| GetMethodInfo(Delegate) |
지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다. |
적용 대상
추가 정보
- 람다 식(C# 프로그래밍 가이드)
- 람다 식: fun 키워드(F#)
- 람다 식
- 대리자(C# 프로그래밍 가이드)
- 대리자(F#)
Visual Basic