Func<T1,T2,TResult> 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
封裝一個包含兩個參數的方法,並回傳由 TResult 參數指定的值。
generic <typename T1, typename T2, typename TResult>
public delegate TResult Func(T1 arg1, T2 arg2);
public delegate TResult Func<in T1,in T2,out TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<in T1,in T2,out TResult>(T1 arg1, T2 arg2) where T1 : allows ref struct where T2 : allows ref struct where TResult : allows ref struct;
public delegate TResult Func<T1,T2,TResult>(T1 arg1, T2 arg2);
type Func<'T1, 'T2, 'Result> = delegate of 'T1 * 'T2 -> 'Result
Public Delegate Function Func(Of In T1, In T2, Out TResult)(arg1 As T1, arg2 As T2) As TResult
Public Delegate Function Func(Of T1, T2, TResult)(arg1 As T1, arg2 As T2) As TResult
類型參數
參數
- arg1
- T1
這個代表所封裝的方法的第一個參數。
- arg2
- T2
這個代表所涵蓋的方法的第二個參數。
傳回值
此代理所封裝的方法的回傳值。
範例
以下範例示範如何宣告並使用 Func<T1,T2,TResult> 代理人。 此範例宣告一個 Func<T1,T2,TResult> 變數,並指派一個 lambda 表達式,該表達式取值 String 與值 Int32 作為參數。 當參數長度String等於參數Int32值時,lambda 運算式會回傳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中,宣告為 Sub 而非 Function 的方法),請使用通用的 Action<T1,T2> 代理。
使用 Func<T1,T2,TResult> 代理時,不必明確定義一個包含兩個參數的方法代理。 例如,以下程式碼明確宣告一個名為代理 ExtractMethod 的代理,並將該方法的參考 ExtractWords 指派給其代理實例。
using System;
delegate string[] ExtractMethod(string stringToManipulate, int maximum);
public class DelegateExample
{
public static void Main()
{
// Instantiate delegate to reference ExtractWords method
ExtractMethod extractMeth = ExtractWords;
string title = "The Scarlet Letter";
// Use delegate instance to call ExtractWords method and display result
foreach (string word in extractMeth(title, 5))
Console.WriteLine(word);
}
private static string[] ExtractWords(string phrase, int limit)
{
char[] delimiters = new char[] {' '};
if (limit > 0)
return phrase.Split(delimiters, limit);
else
return phrase.Split(delimiters);
}
}
type ExtractMethod = delegate of string * int -> string []
let extractWords (phrase: string) limit =
let delimiters = [| ' ' |]
if limit > 0 then
phrase.Split(delimiters, limit)
else
phrase.Split delimiters
// Instantiate delegate to reference extractWords function
let extractMeth = ExtractMethod extractWords
let title = "The Scarlet Letter"
// Use delegate instance to call extractWords function and display result
for word in extractMeth.Invoke(title, 5) do
printfn $"{word}"
' Declare a delegate to represent string extraction method
Delegate Function ExtractMethod(ByVal stringToManipulate As String, _
ByVal maximum As Integer) As String()
Module DelegateExample
Public Sub Main()
' Instantiate delegate to reference ExtractWords method
Dim extractMeth As ExtractMethod = AddressOf ExtractWords
Dim title As String = "The Scarlet Letter"
' Use delegate instance to call ExtractWords method and display result
For Each word As String In extractMeth(title, 5)
Console.WriteLine(word)
Next
End Sub
Private Function ExtractWords(phrase As String, limit As Integer) As String()
Dim delimiters() As Char = {" "c}
If limit > 0 Then
Return phrase.Split(delimiters, limit)
Else
Return phrase.Split(delimiters)
End If
End Function
End Module
以下範例簡化了此程式碼,透過實例化代理 Func<T1,T2,TResult> ,而非明確定義新代理並指派命名方法。
using System;
public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference ExtractWords method
Func<string, int, string[]> extractMethod = ExtractWords;
string title = "The Scarlet Letter";
// Use delegate instance to call ExtractWords method and display result
foreach (string word in extractMethod(title, 5))
Console.WriteLine(word);
}
private static string[] ExtractWords(string phrase, int limit)
{
char[] delimiters = new char[] {' '};
if (limit > 0)
return phrase.Split(delimiters, limit);
else
return phrase.Split(delimiters);
}
}
open System
let extractWords (phrase: string) limit =
let delimiters = [| ' ' |]
if limit > 0 then
phrase.Split(delimiters, limit)
else
phrase.Split delimiters
// Instantiate delegate to reference extractWords function
let extractMethod = Func<string, int, string[]> extractWords
let title = "The Scarlet Letter"
// Use delegate instance to call extractWords function and display result
for word in extractMethod.Invoke(title, 5) do
printfn $"{word}"
Module GenericFunc
Public Sub Main()
' Instantiate delegate to reference ExtractWords method
Dim extractMeth As Func(Of String, Integer, String()) = AddressOf ExtractWords
Dim title As String = "The Scarlet Letter"
' Use delegate instance to call ExtractWords method and display result
For Each word As String In extractMeth(title, 5)
Console.WriteLine(word)
Next
End Sub
Private Function ExtractWords(phrase As String, limit As Integer) As String()
Dim delimiters() As Char = {" "c}
If limit > 0 Then
Return phrase.Split(delimiters, limit)
Else
Return phrase.Split(delimiters)
End If
End Function
End Module
你可以在 C# 中使用匿名方法的 Func<T1,T2,TResult> 代理,如下範例所示。 (關於匿名方法的入門,請參見 匿名方法。)
using System;
public class Anonymous
{
public static void Main()
{
Func<string, int, string[]> extractMeth = delegate(string s, int i)
{ char[] delimiters = new char[] {' '};
return i > 0 ? s.Split(delimiters, i) : s.Split(delimiters);
};
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extractMeth(title, 5))
Console.WriteLine(word);
}
}
你也可以將 lambda 運算式指派給 Func<T1,T2,TResult> 代理,如下範例所示。 (關於 lambda 表達式的入門,請參見 Lambda 表達式(VB)、Lambda 表達式(C#)及 Lambda 表達式(F#)。)
using System;
public class LambdaExpression
{
public static void Main()
{
char[] separators = new char[] {' '};
Func<string, int, string[]> extract = (s, i) =>
i > 0 ? s.Split(separators, i) : s.Split(separators) ;
string title = "The Scarlet Letter";
// Use Func instance to call ExtractWords method and display result
foreach (string word in extract(title, 5))
Console.WriteLine(word);
}
}
open System
let separators = [| ' ' |]
let extract =
Func<string, int, string []> (fun s i ->
if i > 0 then
s.Split(separators, i)
else
s.Split separators)
let title = "The Scarlet Letter"
// Use Func instance to call lambda expression and display result
for word in extract.Invoke(title, 5) do
printfn $"{word}"
Module LambdaExpression
Public Sub Main()
Dim separators() As Char = {" "c}
Dim extract As Func(Of String, Integer, String()) = Function(s, i) _
CType(iif(i > 0, s.Split(separators, i), s.Split(separators)), String())
Dim title As String = "The Scarlet Letter"
For Each word As String In extract(title, 5)
Console.WriteLine(word)
Next
End Sub
End Module
lambda 表達式的底層類型是其中一種通用 Func 代理。 這使得可以將 lambda 運算式作為參數傳遞,而不必明確指派給代理。 特別是,因為命名空間中 System.Linq 許多型別的方法都有 Func<T1,T2,TResult> 參數,你可以在不明確實 Func<T1,T2,TResult> 例化代理的情況下,將 lambda 表達式傳遞給這些方法。
擴充方法
| 名稱 | Description |
|---|---|
| GetMethodInfo(Delegate) |
取得一個代表指定代理所代表方法的物件。 |