Func是一个.Net内置的委托。
Func<Result>,Func<T1,Result>是一个.Net内置的泛型委托。
它有5种形式,只是参数个数不同;第一个是无参数,但是有返回值;
下面是一个简单的普通委托来传方法的示例。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
 | 
private delegate string Say();public static string SayHello(){    return "Hello";}static void Main(string[] args){    Say say = SayHello;    Console.WriteLine(say());    Console.ReadKey();} | 
所以,在有时候,我们不知道一个接口同时要做什么操作的时候,我可以给它留一个委托。
为了更方便,.Net直接默认有了委托。我们再来试试.Net默认带的委托。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
 | 
public static string SayHello(){    return "Hello";}static void Main(string[] args){    Func<string> say = SayHello;    Console.WriteLine(say());    Console.ReadKey();} | 
如果需要参数的,还可以这样传一份。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
 | 
public static string SayHello(string str){    return str + str;}static void Main(string[] args){    Func<string, string> say = SayHello;    string str = say("abc");       Console.WriteLine(str);     //输出abcabc    Console.ReadKey();} | 
Action<T>的用法与Func几乎一样,调用方法也类似。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
 | 
private delegate string Say();public static void SayHello(string str){    Console.WriteLine(str);}static void Main(string[] args){    Action<string> say = SayHello;    say("abc");    Console.ReadKey();} | 
Func与Action作用几乎一样。只是
还是以一个输入后,返回重复一次的值作为示例。
| 
 1 
2 
 | 
Func<string, string> say = m => m + m;Console.WriteLine(say("abc"));    //输出abcabc | 
通常我们最常见到Func是在方法的参数里如下面这样:
| 
 1 
 | 
string XXX(Func<string, string>) | 
咱们来看看Linq里面的其中一个Sum:
| 
 1 
 | 
public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector); | 
里面看到两点:
1、扩展方法,与这篇文章无关(扩展的是IEnumerable<TSource>,主要是为了能够实现IEnumerable<TSource>接口的集合.出函数)。
2、Func<TSource, int> selector这个参数。
尝试写一个Linq的First函数吧,命名为First2。Linq源代码里有很多异常情况处理,好多设计模式,可惜我不懂,只提取简单逻辑了。
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
 | 
namespace ConsoleApplication2{    static class Extend    {        public static TSource First2<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)        {            //.Net本身的源代码好多异常情况处理,好多设计模式,我也不懂,只提取逻辑            foreach (TSource item in source)            {                if (predicate(item))                {                    return (item);                }            }            throw new Exception("不存在满足条件的第一个元素!");        }    }    class Program    {        static void Main(string[] args)        {            List<int> ListInt = new List<int>(){ 1, 2, 3, 4, 5 };            int k = ListInt.First2(m => m > 4);     //输出5            Console.WriteLine(k);            Console.ReadKey();        }    }} | 
原文:http://www.cnblogs.com/dongwenfei/p/6323200.html