Func<T,TResult> 代理人

定義

封裝一個只有一個參數的方法,並回傳由該 TResult 參數指定的類型值。

generic <typename T, typename TResult>
public delegate TResult Func(T arg);
public delegate TResult Func<in T,out TResult>(T arg);
public delegate TResult Func<in T,out TResult>(T arg) where T : allows ref struct where TResult : allows ref struct;
public delegate TResult Func<T,TResult>(T arg);
type Func<'T, 'Result> = delegate of 'T -> 'Result
Public Delegate Function Func(Of In T, Out TResult)(arg As T) As TResult 
Public Delegate Function Func(Of T, TResult)(arg As T) As TResult 

類型參數

T

此代理所封裝的方法參數類型。

這是反變數的型別參數。 也就是說,您可以使用您指定的類型,或衍生程度較低的任何類型。 如需共變數與反變數的詳細資訊,請參閱泛型中的共變數與反變數
TResult

此代理所封裝的方法的回傳值類型。

這是共變數的型別參數。 也就是說,您可以使用您指定的類型,或衍生程度較高的任何類型。 如需共變數與反變數的詳細資訊,請參閱泛型中的共變數與反變數

參數

arg
T

此代理所封裝的方法參數。

傳回值

TResult

此代理所封裝的方法的回傳值。

範例

以下範例示範如何宣告並使用 Func<T,TResult> 代理人。 這個範例宣告一個 Func<T,TResult> 變數,並指派一個 lambda 運算式,將字串中的字元轉換為大寫。 封裝此方法的代理接著會被傳給 Enumerable.Select 該方法,將字串陣列中的字串改為大寫。

// Declare a Func variable and assign a lambda expression to the
// variable. The method takes a string and converts it to uppercase.
Func<string, string> selector = str => str.ToUpper();

// Create an array of strings.
string[] words = { "orange", "apple", "Article", "elephant" };
// Query the array and select strings according to the selector method.
IEnumerable<String> aWords = words.Select(selector);

// Output the results to the console.
foreach (String word in aWords)
    Console.WriteLine(word);

/*
This code example produces the following output:

ORANGE
APPLE
ARTICLE
ELEPHANT

*/
open System
open System.Linq

// Declare a Func variable and assign a lambda expression to the
// variable. The function takes a string and converts it to uppercase.
let selector = Func<string, string>(fun str -> str.ToUpper())

// Create a list of strings.
let words = [ "orange"; "apple"; "Article"; "elephant" ]

// Query the list and select strings according to the selector function.
let aWords = words.Select selector

// Output the results to the console.
for word in aWords do
    printfn $"{word}"

// This code example produces the following output:
//     ORANGE
//     APPLE
//     ARTICLE
//     ELEPHANT
Imports System.Collections.Generic
Imports System.Linq

Module Func
   Public Sub Main()
      ' Declare a Func variable and assign a lambda expression to the  
      ' variable. The method takes a string and converts it to uppercase.
      Dim selector As Func(Of String, String) = Function(str) str.ToUpper()
   
      ' Create an array of strings.
      Dim words() As String = { "orange", "apple", "Article", "elephant" }
      ' Query the array and select strings according to the selector method.
      Dim aWords As IEnumerable(Of String) = words.Select(selector)
   
      ' Output the results to the console.
      For Each word As String In aWords
         Console.WriteLine(word)
      Next
   End Sub
End Module
' This code example produces the following output:
'           
'   ORANGE
'   APPLE
'   ARTICLE
'   ELEPHANT

備註

你可以用這個代理子來表示一個方法,可以直接以參數方式傳遞,而不必明確宣告自訂代理。 封裝方法必須對應於此代理所定義的方法簽名。 這表示封裝方法必須有一個參數,該參數會被傳遞給它,並且必須回傳一個值。

備註

若要參考一個只有一個參數且回傳 void(或在Visual Basic中,宣告為 Sub 而非 Function),請使用通用的 Action<T> 代理。

使用 Func<T,TResult> 代理時,不必明確定義一個以單一參數封裝方法的代理。 例如,以下程式碼明確宣告一個名為代理 ConvertMethod 的代理,並將該方法的參考 UppercaseString 指派給其代理實例。

using System;

delegate string ConvertMethod(string inString);

public class DelegateExample
{
   public static void Main()
   {
      // Instantiate delegate to reference UppercaseString method
      ConvertMethod convertMeth = UppercaseString;
      string name = "Dakota";
      // Use delegate instance to call UppercaseString method
      Console.WriteLine(convertMeth(name));
   }

   private static string UppercaseString(string inputString)
   {
      return inputString.ToUpper();
   }
}
type ConvertMethod = delegate of string -> string

let uppercaseString (inputString: string) =
    inputString.ToUpper()
    
// Instantiate delegate to reference uppercaseString function
let convertMeth = ConvertMethod uppercaseString
let name = "Dakota"

// Use delegate instance to call uppercaseString function
printfn $"{convertMeth.Invoke name}"
' Declare a delegate to represent string conversion method
Delegate Function ConvertMethod(ByVal inString As String) As String

Module DelegateExample
   Public Sub Main()
      ' Instantiate delegate to reference UppercaseString method
      Dim convertMeth As ConvertMethod = AddressOf UppercaseString
      Dim name As String = "Dakota"
      ' Use delegate instance to call UppercaseString method
      Console.WriteLine(convertMeth(name))
   End Sub

   Private Function UppercaseString(inputString As String) As String
      Return inputString.ToUpper()
   End Function
End Module

以下範例簡化了此程式碼,透過實 Func<T,TResult> 例化代理,而非明確定義新代理並指派命名方法。

// Instantiate delegate to reference UppercaseString method
Func<string, string> convertMethod = UppercaseString;
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));

string UppercaseString(string inputString)
{
   return inputString.ToUpper();
}

// This code example produces the following output:
//
//    DAKOTA
open System

let uppercaseString (inputString: string) =
    inputString.ToUpper()

// Instantiate delegate to reference uppercaseString function
let convertMethod = Func<string, string> uppercaseString
let name = "Dakota"

// Use delegate instance to call uppercaseString function
printfn $"{convertMethod.Invoke name}"

// This code example produces the following output:
//    DAKOTA
Module GenericFunc
   Public Sub Main()
      ' Instantiate delegate to reference UppercaseString method
      Dim convertMethod As Func(Of String, String) = AddressOf UppercaseString
      Dim name As String = "Dakota"
      ' Use delegate instance to call UppercaseString method
      Console.WriteLine(convertMethod(name))
   End Sub

   Private Function UppercaseString(inputString As String) As String
      Return inputString.ToUpper()
   End Function
End Module

你也可以在 C# 中用匿名方法使用 Func<T,TResult> 代理,如下範例所示。 (關於匿名方法的入門,請參見 匿名方法。)

 Func<string, string> convert = delegate(string s)
    { return s.ToUpper();};

 string name = "Dakota";
 Console.WriteLine(convert(name));

// This code example produces the following output:
//
//    DAKOTA

你也可以將 lambda 運算式指派給 Func<T,TResult> 代理,如下範例所示。 (關於 lambda 表達式的入門,請參見 Lambda 表達式(VB)、Lambda 表達式(C#)Lambda 表達式(F#)。)

Func<string, string> convert = s => s.ToUpper();

string name = "Dakota";
Console.WriteLine(convert(name));

// This code example produces the following output:
//
//    DAKOTA
open System

let convert = Func<string, string>(fun s -> s.ToUpper())

let name = "Dakota"
printfn $"{convert.Invoke name}"

// This code example produces the following output:
//    DAKOTA
Module LambdaExpression
   Public Sub Main()
      Dim convert As Func(Of String, String) = Function(s) s.ToUpper()
      
      Dim name As String = "Dakota"
      Console.WriteLine(convert(name))  
   End Sub
End Module

lambda 表達式的底層類型是其中一種通用 Func 代理。 這使得可以將 lambda 運算式作為參數傳遞,而不必明確指派給代理。 特別是,因為命名空間中 System.Linq 許多型別的方法都有 Func<T,TResult> 參數,你可以在不明確實 Func<T,TResult> 例化代理的情況下,將 lambda 表達式傳遞給這些方法。

擴充方法

名稱 Description
GetMethodInfo(Delegate)

取得一個代表指定代理所代表方法的物件。

適用於

另請參閱