Fun委托的声明:
public delegate TR Func<TR>();
public delegate TR Func<T0,TR>(T0,a0);
public delegate TR Func<T0,T1,TR>(T0 a0,T1 a1);
public delegate TR Func <T0,T1,T2,TR>(T0 a0,T1 a1,T2 a2);
public delegate TR Func <T0,T1,T2,T3 TR>(T0 a0,T1 a1,T2 a2,T3 a3);
在每个声明中,TR表示返回的数据类型,请注意,在加载每个Func delegate时,返回类型参数TR都在参数类型模板的最后一个,其它类型参数T0,T1,T2,T3指的是传递给该方法的输入参数,多个声明的存在是因为有些标准查询操作符中的委托(delegate)需要比其它操作符更多的参数
以where操作符的一个原型为例:
public static IEnumerable<T> where<T>(this IEnumerable<T> source,Func<T,bool> predicate);
在这个原型定义中,谓词参数被指定为Func<T,bool>通过这样做,开发者可以看出谓词方法或lambda表达式最好只接受单个参数,T参数,并返回bool型值,以下是一个例子
int[] ints=new int[]{1,2,3,4,5,6};
//声明delegate
Func<int,bool> GreaterThanTwo=i=>i>2
IEnumerable<int> intsGreaterThenTwo=ints.where(GreaterThanTwo)
正因为有扩展方法要求第一个参数带this,所以一般是指当前对象,不需要输入参数