1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Lambda演化过程
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 //lambda表达式的演变过程
14 //下面是C# 1.0 中创建委托实例的代码
15 Func<string, int> delegatetest1 = new Func<string, int>(Callbackmethod);
16 //
17 //C# 2.0 中使用匿名方法来创建委托实例,此时,就不需要去额外的定义回调方法Callbackemethod了
18 Func<string, int> delegatetest2 = delegate(string text)
19 {
20 return text.Length;
21 };
22
23 //c# 3.0 中使用Lambda表达式来创建委托实例
24 Func<string, int> delegatetest3 = (string text) => text.Length;
25
26 //也可以省略参数类型 string,从而将代码再次简化
27 Func<string, int> delegatetest4 = (text) => text.Length;
28
29 //也可以省略圆括号
30 Func<string, int> delegatetest = text => text.Length;
31
32 //调用委托
33 Console.WriteLine("调用Lambda表达式返回字符串的长度为:"+delegatetest("learning hard"));
34 Console.Read();
35
36
37 }
38
39
40 private static int Callbackmethod(string text)
41 {
42 return text.Length;
43 }
44 }
45 }