Func<T1,T2,TResult> 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
封裝具有 2 個參數並傳回 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<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
類型參數
- TResult
這個委派所封裝之方法的傳回值之類型。
這是共變數的型別參數。 也就是說,您可以使用您指定的類型,或衍生程度較高的任何類型。 如需共變數與反變數的詳細資訊,請參閱泛型中的共變數與反變數。參數
- arg1
- T1
由這個委派所封裝之方法的第一個參數。
- arg2
- T2
此委派封裝之方法的第二個參數。
傳回值
- TResult
這個委派所封裝之方法的傳回值。
範例
下列範例示範如何宣告和使用 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
備註
您可以使用此委派來表示可以當做參數傳遞的方法,而不需明確宣告自訂委派。 封裝的方法必須對應至這個委派所定義的方法簽章。 這表示封裝的方法必須有兩個參數,每個參數都會以傳值方式傳遞給它,而且必須傳回值。
注意
若要參考具有兩個參數的方法,並在 F#) (或 Visual Basic 中傳回 void
(unit
,該方法宣告為 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> 委派,如下列範例所示。 (如需匿名方法的簡介,請參閱 Anonymous Methods.)
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> 參數,所以您可以傳遞這些方法 Lambda 運算式,而不需要明確具現化 Func<T1,T2,TResult> 委派。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |