Func<TResult> 대리자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
매개 변수가 없는 메서드를 캡슐화하고 매개 변수로 지정된 TResult 형식의 값을 반환합니다.
generic <typename TResult>
public delegate TResult Func();
public delegate TResult Func<out TResult>();
public delegate TResult Func<out TResult>() where TResult : allows ref struct;
public delegate TResult Func<TResult>();
type Func<'Result> = delegate of unit -> 'Result
Public Delegate Function Func(Of Out TResult)() As TResult
Public Delegate Function Func(Of TResult)() As TResult
형식 매개 변수
- TResult
이 대리자가 캡슐화하는 메서드의 반환 값 형식입니다.
이 형식 매개 변수는 공변(Covariant)입니다. 즉, 지정한 형식이나 더 많게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.반환 값
이 대리자가 캡슐화하는 메서드의 반환 값입니다.
예제
다음 예제에서는 매개 변수를 사용하지 않는 대리자를 사용하는 방법을 보여 줍니다. 이 코드는 형식 Func<TResult>필드가 있는 제 LazyValue 네릭 클래스를 만듭니다. 이 대리자 필드는 개체의 형식 매개 변수에 해당하는 형식의 값을 반환하는 모든 함수에 대한 참조를 LazyValue 저장할 수 있습니다. 이 형식에는 LazyValueValue 함수를 실행하고(아직 실행되지 않은 경우) 결과 값을 반환하는 속성도 있습니다.
이 예제에서는 두 개의 메서드를 만들고 이러한 메서드를 호출하는 람다 식을 사용하여 두 LazyValue 개체를 인스턴스화합니다. 람다 식은 메서드를 호출하기만 하면 되므로 매개 변수를 사용하지 않습니다. 출력에서 알 수 있듯이 두 메서드는 각 LazyValue 개체의 값을 검색할 때만 실행됩니다.
using System;
static class Func1
{
public static void Main()
{
// Note that each lambda expression has no parameters.
LazyValue<int> lazyOne = new LazyValue<int>(() => ExpensiveOne());
LazyValue<long> lazyTwo = new LazyValue<long>(() => ExpensiveTwo("apple"));
Console.WriteLine("LazyValue objects have been created.");
// Get the values of the LazyValue objects.
Console.WriteLine(lazyOne.Value);
Console.WriteLine(lazyTwo.Value);
}
static int ExpensiveOne()
{
Console.WriteLine("\nExpensiveOne() is executing.");
return 1;
}
static long ExpensiveTwo(string input)
{
Console.WriteLine("\nExpensiveTwo() is executing.");
return (long)input.Length;
}
}
class LazyValue<T> where T : struct
{
private Nullable<T> val;
private Func<T> getValue;
// Constructor.
public LazyValue(Func<T> func)
{
val = null;
getValue = func;
}
public T Value
{
get
{
if (val == null)
// Execute the delegate.
val = getValue();
return (T)val;
}
}
}
/* The example produces the following output:
LazyValue objects have been created.
ExpensiveOne() is executing.
1
ExpensiveTwo() is executing.
5
*/
open System
type LazyValue<'T>(func: Func<'T>) =
let mutable value = ValueNone
member _.Value =
match value with
| ValueSome v -> v
| ValueNone ->
// Execute the delegate.
let v = func.Invoke()
value <- ValueSome v
v
let expensiveOne () =
printfn "\nExpensiveOne() is executing."
1
let expensiveTwo (input: string) =
printfn "\nExpensiveTwo() is executing."
int64 input.Length
// Note that each lambda expression has no parameters.
let lazyOne = LazyValue(fun () -> expensiveOne ())
let lazyTwo = LazyValue(fun () -> expensiveTwo "apple")
printfn "LazyValue objects have been created."
// Get the values of the LazyValue objects.
printfn $"{lazyOne.Value}"
printfn $"{lazyTwo.Value}"
// The example produces the following output:
// LazyValue objects have been created.
//
// ExpensiveOne() is executing.
// 1
//
// ExpensiveTwo() is executing.
// 5
Public Module Func
Public Sub Main()
' Note that each lambda expression has no parameters.
Dim lazyOne As New LazyValue(Of Integer)(Function() ExpensiveOne())
Dim lazyTwo As New LazyValue(Of Long)(Function() ExpensiveTwo("apple"))
Console.WriteLine("LazyValue objects have been created.")
' Get the values of the LazyValue objects.
Console.WriteLine(lazyOne.Value)
Console.WriteLine(lazyTwo.Value)
End Sub
Public Function ExpensiveOne() As Integer
Console.WriteLine()
Console.WriteLine("ExpensiveOne() is executing.")
Return 1
End Function
Public Function ExpensiveTwo(input As String) As Long
Console.WriteLine()
Console.WriteLine("ExpensiveTwo() is executing.")
Return input.Length
End Function
End Module
Public Class LazyValue(Of T As Structure)
Private val As Nullable(Of T)
Private getValue As Func(Of T)
' Constructor.
Public Sub New(func As Func(Of T))
Me.val = Nothing
Me.getValue = func
End Sub
Public ReadOnly Property Value() As T
Get
If Me.val Is Nothing Then
' Execute the delegate.
Me.val = Me.getValue()
End If
Return CType(val, T)
End Get
End Property
End Class
설명
이 대리자를 사용하여 사용자 지정 대리자를 명시적으로 선언하지 않고 매개 변수로 전달할 수 있는 메서드를 나타낼 수 있습니다. 캡슐화된 메서드는 이 대리자가 정의한 메서드 서명에 해당해야 합니다. 즉, 캡슐화된 메서드에는 매개 변수가 없어야 하며 값을 반환해야 합니다.
메모
매개 변수가 없고 void(unit, F#) 또는 Visual Basic Function 대신 Sub 선언된 메서드를 참조하려면 대신 Action 대리자를 사용합니다.
대리자를 Func<TResult> 사용하는 경우 매개 변수가 없는 메서드를 캡슐화하는 대리자를 명시적으로 정의할 필요가 없습니다. 예를 들어 다음 코드는 명명된 WriteMethod 대리자를 명시적으로 선언하고 해당 대리자 인스턴스에 인스턴스 메서드에 OutputTarget.SendToFile 대한 참조를 할당합니다.
using System;
using System.IO;
delegate bool WriteMethod();
public class TestDelegate
{
public static void Main()
{
OutputTarget output = new OutputTarget();
WriteMethod methodCall = output.SendToFile;
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
open System.IO
type WriteMethod = delegate of unit -> bool
type OutputTarget() =
member _.SendToFile() =
try
let fn = Path.GetTempFileName()
use sw = new StreamWriter(fn)
sw.WriteLine "Hello, World!"
true
with _ ->
false
let output = new OutputTarget()
let methodCall = WriteMethod output.SendToFile
if methodCall.Invoke() then
printfn "Success!"
else
printfn "File write operation failed."
Imports System.IO
Delegate Function WriteMethod As Boolean
Module TestDelegate
Public Sub Main()
Dim output As New OutputTarget()
Dim methodCall As WriteMethod = AddressOf output.SendToFile
If methodCall() Then
Console.WriteLine("Success!")
Else
Console.WriteLine("File write operation failed.")
End If
End Sub
End Module
Public Class OutputTarget
Public Function SendToFile() As Boolean
Try
Dim fn As String = Path.GetTempFileName
Dim sw As StreamWriter = New StreamWriter(fn)
sw.WriteLine("Hello, World!")
sw.Close
Return True
Catch
Return False
End Try
End Function
End Class
다음 예제에서는 새 대리자를 명시적으로 정의하고 명명된 메서드를 할당하는 Func<TResult> 대신 대리자를 인스턴스화하여 이 코드를 간소화합니다.
using System;
using System.IO;
public class TestDelegate
{
public static void Main()
{
OutputTarget output = new OutputTarget();
Func<bool> methodCall = output.SendToFile;
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
open System
open System.IO
type OutputTarget() =
member _.SendToFile() =
try
let fn = Path.GetTempFileName()
use sw = new StreamWriter(fn)
sw.WriteLine "Hello, World!"
true
with _ ->
false
let output = OutputTarget()
let methodCall = Func<bool> output.SendToFile
if methodCall.Invoke() then
printfn "Success!"
else
printfn "File write operation failed."
Imports System.IO
Module TestDelegate
Public Sub Main()
Dim output As New OutputTarget()
Dim methodCall As Func(Of Boolean) = AddressOf output.SendToFile
If methodCall() Then
Console.WriteLine("Success!")
Else
Console.WriteLine("File write operation failed.")
End If
End Sub
End Module
Public Class OutputTarget
Public Function SendToFile() As Boolean
Try
Dim fn As String = Path.GetTempFileName
Dim sw As StreamWriter = New StreamWriter(fn)
sw.WriteLine("Hello, World!")
sw.Close
Return True
Catch
Return False
End Try
End Function
End Class
다음 예제와 같이 C#에서 익명 메서드와 함께 대리자를 사용할 Func<TResult> 수 있습니다. (익명 메서드에 대한 소개는 익명 메서드를 참조하세요.)
using System;
using System.IO;
public class Anonymous
{
public static void Main()
{
OutputTarget output = new OutputTarget();
Func<bool> methodCall = delegate() { return output.SendToFile(); };
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
다음 예제와 같이 대리자에도 람다 식을 할당할 Func<T,TResult> 수 있습니다. 람다 식에 대한 소개는 람다 식(VB), 람다 식(C#) 및 람다 식(F#)을 참조하세요.
using System;
using System.IO;
public class Anonymous
{
public static void Main()
{
OutputTarget output = new OutputTarget();
Func<bool> methodCall = () => output.SendToFile();
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
open System
open System.IO
type OutputTarget() =
member _.SendToFile() =
try
let fn = Path.GetTempFileName()
use sw = new StreamWriter(fn)
sw.WriteLine "Hello, World!"
true
with _ ->
false
let output = OutputTarget()
let methodCall = Func<bool>(fun () -> output.SendToFile())
if methodCall.Invoke() then
printfn "Success!"
else
printfn "File write operation failed."
Imports System.IO
Module TestDelegate
Public Sub Main()
Dim output As New OutputTarget()
Dim methodCall As Func(Of Boolean) = Function() output.SendToFile()
If methodCall() Then
Console.WriteLine("Success!")
Else
Console.WriteLine("File write operation failed.")
End If
End Sub
End Module
Public Class OutputTarget
Public Function SendToFile() As Boolean
Try
Dim fn As String = Path.GetTempFileName
Dim sw As StreamWriter = New StreamWriter(fn)
sw.WriteLine("Hello, World!")
sw.Close
Return True
Catch
Return False
End Try
End Function
End Class
람다 식의 기본 형식은 제네릭 Func 대리자 중 하나입니다. 이렇게 하면 대리자에게 명시적으로 할당하지 않고 람다 식을 매개 변수로 전달할 수 있습니다. 특히 네임스페이스의 많은 형식 메서드에는 System.LinqFunc 매개 변수가 있으므로 대리자를 명시적으로 인스턴스화하지 않고도 이러한 메서드를 람다 식으로 Func 전달할 수 있습니다.
실제로 결과가 필요한 경우에만 실행하려는 비용이 많이 드는 계산이 있는 경우 대리자에게 비용이 많이 드는 함수를 Func<TResult> 할당할 수 있습니다. 그런 다음, 값에 액세스하는 속성이 식에 사용될 때까지 함수 실행을 지연할 수 있습니다. 다음 섹션의 예제에서는 이 작업을 수행하는 방법을 보여 줍니다.
확장명 메서드
| Name | Description |
|---|---|
| GetMethodInfo(Delegate) |
지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다. |
적용 대상
추가 정보
- 람다 식(C# 프로그래밍 가이드)
- 람다 식: fun 키워드(F#)
- 람다 식
- 대리자(C# 프로그래밍 가이드)
- 대리자(F#)
Visual Basic