C#委托事件详解第三篇

[来源] 达内    [编辑] 达内   [时间]2012-09-18

这篇博客用委托简单的实现了几个小案列,如:委托实现多线程,最后讲述了匿名方法和委托链。

前言: 这篇博客用委托简单的实现了几个小案列,如:委托实现多线程,最后讲述了匿名方法和委托链。

  1. 委托实现多线程

(1)我们经常听到使用多线程,所以多线程执行的是什么?很简单,就是执行方法

(2)举个例子,完成输出一句I Love YOu,随机的切换红色,蓝色,黄色,白色

 

 1

   class Program 
 2

 
 3    { 
 4
 
 5        static
 void Main(string
[] args) 

 6
 
 7        { 
 8
 
 9            Thread thread=new
 Thread(Red); 10
 
11            Thread thread1=new
 Thread(Yellow); 12
 
13            Thread thread2=new
 Thread(Blue); 14
 
15        //
在这里我们将Thread转到元数据的话我们就可以看到这是一个委托实现的方法

16 
17            thread.Start(); 
18
 
19            thread1.Start(); 
20
 
21            thread2.Start(); 
22
 
23        } 
24
 
25        public
 static void
 Red() 

26
 
27        { 
28
 
29            for
(int i=0
;i<10000000

;i++) 30
 
31            { 
32
 
33                Console.ForegroundColor=; 
34
 
35                Console.WriteLine("
I Love You"


); 36
 
37                Console.ForegroundColor=ConsoleColor.White; 
38
 
39            } 
40
 
41        } 
42
 
43        public
 static void
 Yellow() 

44
 
45        { 
46
 
47            for
(int i=0
;i<10000000

;i++) 48
 
49            { 
50
 
51                Console.ForegroundColor=ConsoleColor.Yellow; 
52
 
53                Console.WriteLine("
I Love You"


); 54
 
55                Console.ForegroundColor=ConsoleColor.White; 
56
 
57            } 
58
 
59        } 
60
 
61        public
 static void
 Blue() 

62
 
63        {
65            for
(int i=0
;i<10000000

;i++)

67            {
69                Console.ForegroundColor=;
71                Console.WriteLine("
I Love You"
);
73                Console.ForegroundColor=ConsoleColor.White;
75            }
77        }
79    }

 

  1. 匿名方法和Lambda

(1)我们可以定义两个方法,然后实现委托方法的调用,代码如下:

  

 1

    public delegate
 

void  FuncDelegate();  2
 
 3     class
 Program  4
 
 5     { 
 6
 
 7         static
 void Main(string
[] args) 

 8
 
 9         { 
10
 
11             //
这样我们可以调用第一个方法,

12 
13             Run(Func); 
14
 
15             //
但是我们如何调用第二个方法了,这时候我们可以使用匿名方法

16 
17             Run(FuncExt); 
18
 
19             Console.ReadKey(); 
20
 
21         } 
22
 
23  
24 
25         public
 static void
 Run(FuncDelegate target) 

26
 
27         { 
28
 
29             target(); 
30
 
31         } 
32
 
33  
34 
35         public
 static void
 Func() 

36
 
37         { 
38
 
39             Console.WriteLine("
无参数"
); 

40
 
41         } 
42
 
43  
44 
45         public
 static void
 FuncExt() 

46
 
47         { 
48
 
49             Func(10
); 50
 
51         } 
52         public
 static

 void Func(int
 num) 53
 
54         { 
55
 
56             Console.WriteLine("
数字{0}"


, num); 57
 
58         } 
59
 
60     }

 

     但是这样有时候考虑方法类型较繁琐,并且有些方法只使用一次就不用了,如果我们单独定义方法和委托不太合理,

     浪费资源,所以就有了匿名方法,语法是:delegate(){};

     下面我将上面的方法写成这样:

    

 1

  class Program 
 2

 
 3     { 
 4
 
 5         static
 void Main(string
[] args) 

 6
 
 7         { 
 8
 
 9             //
定义一个委托变量,实现一个匿名方法

10 
11             FuncDelegate MyFunc = delegate
() 12
 
13             { 
14
 
15                 Func(); 
16
 
17                 Func(10
); 18
 
19             }; 
20
 
21             //
由于匿名方法比较灵活使用比较方法,但是还是要写很多代码,就借鉴了函数式编程便有了Lambda表达式 
22

 
23             //
语法: 参数=>方法体

24 
25             FuncDelegate myFunc2 = () => { Func(10
); }; 26
 
27             FuncDelegate myFunc3 = () => Func(10
); 28
 
29             Run(MyFunc); 
30
 
31             Run(myFunc2); 
32
 
33             Run(myFunc3); 
34
 
35             Console.ReadKey(); 
36
 
37         } 
38
 
39         public
 static void
 Run(FuncDelegate target) 

40
 
41         { 
42
 
43             target(); 
44
 
45         } 
46
 
47         public
 static void
 Func() 

48
 
49         { 
50
 
51             Console.WriteLine("
无参数"
); 

52
 
53         } 
54
 
55         public
 static void
 FuncExt() 

56
 
57         { 
58
 
59             Func(10
); 60
 
61         } 
62
 
63         public
 static void
 Func(int

 num) 64
 
65         { 
66
 
67             Console.WriteLine("
数字{0}"


, num); 68
 
69         } 
70
 
71     }

     如果想要学习Lambda表达式的话可以查看MSDN

  1. 多播委托(委托链)

(1)代码解释委托链,详尽的信息都在注释里面了,可以参考一下

 

 1

   public delegate
 

void FuncDelegate();  2
 
 3     class
 Program  4
 
 5     { 
 6
 
 7         static
 void Main(string
[] args) 

 8
 
 9         { 
10
 
11             //
先定义三个方法,在定义一个委托内心,在定义一个委托变量

12 
13             FuncDelegate MyFunc; 
14
 
15             MyFunc = Func1; 
16
 
17             //
什么事委托链呢?就是使用+=添加委托方法

18 
19             MyFunc += Func2; 
20
 
21             MyFunc += Func3; 
22
 
23             //
当然我们可以通过+=添加方法,所以我们还可以使用-=移除方法

24 
25             MyFunc -= Func2; 
26
 
27             //
调用委托变量这依次执行所有添加的方法

28 
29             MyFunc(); 
30
 
31            //
注释:如果在其中一个方法出现异常,其后的方法不再执行

32 
33             Console.ReadKey(); 
34
 
35         } 
36
 
37         static
 void Func1() 
38

 
39         { 
40
 
41             Console.WriteLine("
Func1"
); 

42
 
43         } 
44
 
45         static
 void Func2() 
46

 
47         { 
48
 
49             Console.WriteLine("
Func2"
); 

50
 
51         } 
52
 
53         static
 void Func3() 
54

 
55         { 
56
 
57             Console.WriteLine("
Func3"
); 

58
 
59         } 
60
 
61     }

 

   (2)委托链中添加的方法如果带有返回值,调用委托变量后也会有一个返回值,这个返回值是最后一个方法

   

 1

 public delegate
 int

 FuncIntDelegate();

 3     class
 Program

 5     {
 7         static
 void Main(string
[] args) 9
         {

11             FuncIntDelegate MyFunc = FuncInt1;
13             MyFunc += FuncInt1;
15             MyFunc += FuncInt2;
17             MyFunc += FuncInt3;
19             int
 res = MyFunc();

21             //
如果需要返回其中所有方法的返回值,需要使用循环遍历方法的数组

25             Delegate[] ds = MyFunc.GetInvocationList();  //
返回一个Delegate[]数组

27             for
 (int i = 0
; i < ds.Length; i++)29
             {

31                 FuncIntDelegate temp = (FuncIntDelegate)ds[i];
33                 Console.WriteLine(temp());
35             }
39             Console.WriteLine(res);
41             Console.ReadKey();
43         }
45         static
 int FuncInt1()

47         {
49             return
 1;

51         }
53         static
 int FuncInt2()

55         {
57             return
 2;

59         }
61         static
 int FuncInt3()

63         {
65             return
 3;

67         }
69     }

资源下载