Func 委托重载方法:
public delegate T Func< T >();
public delegate T Func< A0, T >( A0 arg0 );
public delegate T Func<A0, A1, T> ( A0 arg0, A1 arg1 );
public delegate T Func<A0, A1, A2, T >( A0 arg0, A1 arg1, A2 arg2 )
;public delegate T Func<A0, A1, A3, T> ( A0 arg0, A1 arg1, A2 arg2, A3 arg3 );
实例如下:看一lambda写法的变化最后是不是有点类似 linq to sql where(c=>c.id>0)语法哦!
public static void Demo()
{
string[] names = { "Marco", "Paolo", "Tom" };
//写法一
names.Display(
delegate(string s) { return s.Length > 4; }
);
//写法二 (lambda 语句 写法)
names.Display(
(string s) => { return s.Length > 4; }
);
//写法三
names.Display(
(s) => { return s.Length > 4; }
);
//写法四 (lambda 表达式 写法)
names.Display(s => s.Length > 4);
}
public static void Display<T>(this T[] names, Func<T, bool> filter)
{
foreach (T s in names)
{
if (filter(s)) Console.WriteLine(s);
}
}
调用输出结果是:Marco Paolo;