C# Delegate 匿名 Delegate

C#6.0新添加了 lambda的強力支持,用lambda的確能夠節省好多代碼,讓代碼看起來更簡潔,更直觀;spa

這裏作一個筆記,C#的匿名委託 Democode

 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的方法顯然要簡潔不少!blog

僅僅做爲筆記,參考!get

 本文爲七小站主原創做品,轉載請註明出處:http://www.cnblogs.com/qixiaoyizhan/ 且在文章頁面明顯位置給出原文連接,不然保留追究法律責任的權利。string

相關文章
相關標籤/搜索