C# Lamba简化委托

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 namespace lamba简化委托
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             //Action    内置 无参无返回值
12             //1.
13             Action<string, bool> a1 = delegate(string s, bool b) { Console.WriteLine(s); };
14             a1("1", true);
15             //2.
16             Action<string, bool> a2 = (string s, bool b) => { Console.WriteLine(s); };
17             a2("2", true);
18             //3.
19             Action<string, bool> a3 = (s,b) => { Console.WriteLine(s); };
20             a3("3", true);
21 
22 
23             //Func      内置 最后一个参数为返回值
24             //1.
25             Func<int, bool> f1 = delegate(int n) { return n > 0; };
26             f1(10);
27             //2.
28             Func<int, bool> f2 = (int n) => { return n > 0; };
29             f2(-1);
30             //3.
31             Func<int, bool> f3 = n => { return n > 0; };
32             f3(5);
33             //4.
34             Func<int, bool> f4 = n => n > 0;
35             f4(-100);
36             Console.ReadKey();
37         }
38     }
39 }
View Code

 

posted @ 2020-04-15 08:55  潇湘梦白  阅读(174)  评论(0编辑  收藏  举报