System.Func 委托 
方法描述
封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
语法定义(C# System.Func 委托 的用法) 
public delegate TResult Func( T arg ) 
提示和注释
可以使用此委托表示一种能以参数形式传递的方法,而不用显式声明自定义委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有一个通过值传递给它的参数,并且必须返回值。
注意
若要引用具有一个参数并返回 void 的方法(或者要在 Visual Basic 中引用被声明为 Sub 而不是被声明为 Function 的方法),请改用泛型 Action
在使用 Func
C#
VB
复制
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
C#
VB
复制
using System;
public class GenericFunc
{
public static void Main()
{
// Instantiate delegate to reference UppercaseString method
      Func
string name = "Dakota";
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));
}
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
您也可以按照以下示例所演示的那样在 C# 中将 Func
C#
复制
using System;
public class Anonymous
{
public static void Main()
{
      Func
{ return s.ToUpper();};
string name = "Dakota";
Console.WriteLine(convert(name));
}
}
您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Func
C#
VB
复制
using System;
public class LambdaExpression
{
public static void Main()
{
      Func
string name = "Dakota";
Console.WriteLine(convert(name));
}
}
Lambda 表达式的基础类型是泛型 Func 委托之一。 这样能以参数形式传递 lambda 表达式,而不用显式将其分配给委托。 尤其是,因为 System.Linq 命名空间中许多类型方法具有 Func
System.Func 委托例子 
随后将封装此方法的委托传递给 Enumerable.Select 方法,以将字符串数组中的字符串更改为大写。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
static class Func
{
   static void Main(string[] args)
   {
      // Declare a Func variable and assign a lambda expression to the  
      // variable. The method takes a string and converts it to uppercase.
      Func 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 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
*/
  
继承层次结构
线程安全
版本信息
.NET Framework 受以下版本支持:4、3.5 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。