C# Delegate 匿名 Delegate

C#6.0新添加了 lambda的强力支持,用lambda的确可以节省好多代码,让代码看起来更简洁,更直观;

这里做一个笔记,C#的匿名委托 Demo

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5 
 6             List<tb_User> list = new List<tb_User>{
 7                 new tb_User{ Id = 1001, Name = "susan", Age = 10 },
 8                 new tb_User{ Id = 1002, Name = "susan", Age = 10 },
 9                 new tb_User{ Id = 1003, Name = "susan", Age = 10 },
10                 new tb_User{ Id = 1004, Name = "susan4", Age = 10 },
11                 new tb_User{ Id = 1005, Name = "susan5", Age = 10 },
12                 new tb_User{ Id = 1006, Name = "susan6", Age = 17 },
13                 new tb_User{ Id = 1007, Name = "susan7", Age = 18 },
14                 new tb_User{ Id = 1008, Name = "susan8", Age = 19 },
15                 new tb_User{ Id = 1009, Name = "susan9", Age = 20 },
16                 new tb_User{ Id = 1010, Name = "susan10", Age = 21 }
17             };
18 
19             var list1 = list.Find(
20                tb_User => { return tb_User.Age == 18; }
21                );
22 
23             var list2 = list.Find(
24                 delegate (tb_User user){
25                     return user.Age == 18;
26                         }
27                 );
28             Console.WriteLine(list2.Age);
29 
30 
31 
32             Console.ReadKey();
33         }
34     }
35     public class tb_User
36     {
37         public int Id { get; set; }
38         public string Name { get; set; }
39         public int Age { get; set; }
40 
41     }

这里list1和list2实现的是同一个功能,但是用list1的方法显然要简洁很多!

仅仅作为笔记,参考!

 本文为七小站主原创作品,转载请注明出处:http://www.cnblogs.com/qixiaoyizhan/ 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

posted @ 2016-12-03 10:53  7tiny  阅读(1028)  评论(0编辑  收藏  举报