Func<T,TResult> 委托

定义

封装一个方法,该方法具有一个参数,且返回由 TResult 参数指定的类型的值。

C#
public delegate TResult Func<in T,out TResult>(T arg);
C#
public delegate TResult Func<T,TResult>(T arg);

类型参数

T

此委托封装的方法的参数类型。

这是逆变类型参数。 即,可以使用指定的类型,也可以使用派生程度较低的任何类型。 有关协变和逆变的详细信息,请参阅泛型中的协变和逆变
TResult

此委托封装的方法的返回值类型。

这是协变类型参数。 即,可以使用指定的类型,也可以使用派生程度较高的任何类型。 有关协变和逆变的详细信息,请参阅泛型中的协变和逆变

参数

arg
T

此委托封装的方法的参数。

返回值

TResult

此委托封装的方法的返回值。

示例

下面的示例演示如何声明和使用 Func<T,TResult> 委托。 此示例声明一个 Func<T,TResult> 变量,并为其分配一个 lambda 表达式,该表达式将字符串中的字符转换为大写。 然后将封装此方法的委托传递给 Enumerable.Select 方法,以将字符串数组中的字符串更改为大写。

C#
// 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

*/

注解

您可以使用此委托来表示一个方法,该方法可作为参数传递,而无需显式声明自定义委托。 封装的方法必须对应于由此委托定义的方法签名。 这意味着,封装的方法必须具有一个通过值传递给它的参数,并且必须返回一个值。

备注

若要引用一个方法,该方法具有一个参数并返回 void (或 Visual Basic 中声明为 Sub 而不是) 的方法 Function ,请改用泛型 Action<T> 委托。

使用 Func<T,TResult> 委托时,无需显式定义使用单个参数封装方法的委托。 例如,下面的代码显式声明一个名为的委托 ConvertMethod ,并向 UppercaseString 其委托实例分配对该方法的引用。

C#
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();
   }
}

下面的示例通过实例化委托来简化此代码 Func<T,TResult> ,而不是显式定义一个新委托并为其分配一个命名方法。

C#
// 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

你还可以 Func<T,TResult> 在 c # 中将委托与匿名方法一起使用,如下面的示例所示。 (匿名方法的介绍,请参阅 匿名方法。 )

C#
 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 # ) 。 )

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

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

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

Lambda 表达式的基础类型是一个泛型 Func 委托。 这样,便可以将 lambda 表达式作为参数传递,而无需将其显式分配给委托。 具体而言,因为命名空间中的许多类型的方法 System.Linq 都有 Func<T,TResult> 参数,所以可以将这些方法传递给 lambda 表达式,而无需显式实例化 Func<T,TResult> 委托。

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅