委托的应用之Lambda表达式

  首先我们通过下面的代码来看看委托逐步升级简化的过程。

 1 namespace LambdaDemo
 2 {
 3     class Program
 4     {
 5         delegate void ShowMethod(string name);
 6         static void Main(string[] args)
 7         {
 8             Test("star");
 9             Console.ReadLine();
10         }
11 
12         public static void Test(string Name)
13         {
14             //.net framework 1.1 委托正常调用
15             {
16                 ShowMethod method = new ShowMethod(DoSomething);
17                 method.Invoke(Name);
18             }
19             //.net framework 2.0
20             {
21                 ShowMethod method = new ShowMethod(
22                     delegate(string name)
23                     {
24                         Console.WriteLine("Hello ---- " + name);
25                     }//匿名方法
26                     );
27                 method.Invoke(Name);
28             }
29             //.net framework 3.0 (linq)
30             {
31                 ShowMethod method = new ShowMethod(
32                    (string name) => //lambda 匿名方法,其实也就是个方法 参数列表=>方法体
33                    {
34                        Console.WriteLine("Hello ---- " + name);
35                    }
36                     );
37                 method.Invoke(Name);
38             }
39 
40             {
41                 ShowMethod method = new ShowMethod(
42                    (name) => //参数类型可以去掉,因为委托的约束。
43                    {
44                        Console.WriteLine("Hello ---- " + name);
45                    }
46                     );
47                 method.Invoke(Name);
48             }
49             //如果方法体只有一行,则大括号和分号可以去掉
50             {
51                 ShowMethod method = new ShowMethod(
52                    (name) =>  Console.WriteLine("Hello ---- " + name)
53                    );
54                 method.Invoke(Name);
55             }
56             //实例化委托可以简写 最终简化版
57             {
58                 ShowMethod method = (name) => Console.WriteLine("Hello ---- " + name);
59                 method.Invoke(Name);
60             }
61         }
62 
63         private static void DoSomething(string name)
64         {
65             Console.WriteLine("Hello ---- " + name);
66         }
67     }
68 }
View Code

  下面我介绍两个.net 自带的委托 Action ,Func

 

 1             Console.WriteLine("-----------Action-------");
 2             //Action 无返回值 接受0 到16个泛型参数的委托
 3             {
 4                 Action act1 = () => Console.WriteLine("Hello ---- ");
 5                 act1.Invoke();
 6                 Action<string> act2 = (t) => Console.WriteLine("Hello ---- " + t);
 7                 act2.Invoke("star");
 8                 //如果只有一个参数可以去掉小括号
 9                 Action<string> act3 = t => Console.WriteLine("Hello ---- " + t);
10                 act2.Invoke("star");
11                 Action<string, int, DateTime> act4 = (p1, p2, p3) => Console.WriteLine("Hello ---- ");
12             }
13             Console.WriteLine("-----------Action-------");
14             //Func 有返回值 接受0 到16个泛型参数的委托
15             {
16                 Func<int> func = () =>
17                     {
18                         int d = DateTime.Now.Day;
19                         return d;
20                     };
21                 int r = func.Invoke();
22                 Func<int> func1 = () => DateTime.Now.Day; //方法体只有一行可以去掉大括号,return
23                 Func<int, string> func2 = (p1) => p1.ToString();
24                 Func<int, DateTime, string> func3 = (p1, p2) => p2.ToString();
25             }

 Lambda 表达式

 1 {
 2                 List<int> arr = new List<int>();
 3                 arr.Add(1);
 4                 arr.Add(2);
 5                 arr.Add(3);
 6                 arr.Add(4);
 7                 arr.Add(5);
 8                 arr.Add(6);
 9 
10 
11                 //命名方法
12                 List<int> arr1 = arr.Where(GetNumList).ToList();
13                 //匿名方法
14                 List<int> arr2 = arr.Where(new Func<int, bool>(delegate(int t)
15                 {
16                     return t % 2 == 0;
17                 })).ToList();
18                 //匿名方法
19                 List<int> arr3 = arr.Where(delegate(int t)
20                 {
21                     return t % 2 == 0;
22                 }).ToList();
23                 //Lamada 表达式 
24                 List<int> arr4 = arr.Where(t => t % 2 == 0).ToList();
25             }
26 
27 
28  private static bool GetNumList(int n)
29         {
30             return n % 2 == 0;
31         }

总结

lambda 表达式 就是一个匿名方法,就是简单化了委托的声明和调用,从而精简了代码量。Lambda运算符=>,该运算符读作"goes to"。

posted @ 2017-05-25 11:46  一叶青城  阅读(161)  评论(0编辑  收藏  举报