System.Func 委托 
方法描述
封装一个不具有参数但却返回 TResult 参数指定的类型值的方法。
语法定义(C# System.Func 委托 的用法) 
public delegate TResult Func() 
提示和注释
可以使用此委托表示一种能以参数形式传递的方法,而不用显式声明自定义委托。 封装的方法必须与此委托定义的方法签名相对应。 这意味着封装的方法不得具有参数,但必须返回值。
注意
若要引用不具有参数但却返回 void 的方法(或者在 Visual Basic 中,被声明为 Sub 而不是被声明为 Function 的方法),请改用 Action 委托。
在使用 Func
C#
VB
复制
using System;
using System.IO;
delegate bool WriteMethod();
public class TestDelegate
{
public static void Main()
{
OutputTarget output = new OutputTarget();
WriteMethod methodCall = output.SendToFile;
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
以下示例简化了此代码,它所用的方法是实例化 Func
C#
VB
复制
using System;
using System.IO;
public class TestDelegate
{
public static void Main()
{
OutputTarget output = new OutputTarget();
      Func
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
您可以按照以下示例所演示的那样在 C# 中将 Func
C#
复制
using System;
using System.IO;
public class Anonymous
{
public static void Main()
{
OutputTarget output = new OutputTarget();
      Func
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Func
C#
VB
复制
using System;
using System.IO;
public class Anonymous
{
public static void Main()
{
OutputTarget output = new OutputTarget();
      Func
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
}
public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
Lambda 表达式的基础类型是泛型 Func 委托之一。 这样能以参数形式传递 lambda 表达式,而不用显式将其分配给委托。 尤其是,因为 System.Linq 命名空间中许多类型方法具有 Func 参数,因此可以给这些方法传递 lambda 表达式,而不用显式实例化 Func 委托。
如果具有只在实际需要结果时才要执行的高开销计算,则可以将高开销函数分配给 Func
System.Func 委托例子 
正如输出所显示的那样,只有当检索到每个 LazyValue 对象的值时才会执行这两个方法。
using System;
static class Func1
{
   public static void Main()
   {
      // Note that each lambda expression has no parameters.
      LazyValue lazyOne = new LazyValue(() => ExpensiveOne());
      LazyValue lazyTwo = new LazyValue(() => ExpensiveTwo("apple"));
      Console.WriteLine("LazyValue objects have been created.");
      // Get the values of the LazyValue objects.
      Console.WriteLine(lazyOne.Value);
      Console.WriteLine(lazyTwo.Value);
   }
   static int ExpensiveOne()
   {
      Console.WriteLine("\nExpensiveOne() is executing.");
      return 1;
   }
   static long ExpensiveTwo(string input)
   {
      Console.WriteLine("\nExpensiveTwo() is executing.");
      return (long)input.Length;
   }
}
class LazyValue where T : struct
{
   private Nullable val;
   private Func getValue;
   // Constructor.
   public LazyValue(Func func)
   {
      val = null;
      getValue = func;
   }
   public T Value
   {
      get
      {
         if (val == null)
            // Execute the delegate.
            val = getValue();
         return (T)val;
      }
   }
}
/* The example produces the following output:
    LazyValue objects have been created.
    ExpensiveOne() is executing.
    1
    ExpensiveTwo() is executing.
    5
*/
        
继承层次结构
线程安全
版本信息
.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 系统要求。