高级部分_委托、Lambda表达式、事件

委托

(1)把方法当作参数来传递的话,就要用到委托;

(2)委托是一个类型,这个类型可以赋值一个方法的引用。

C#使用一个类分为两个阶段,首先定义这个类,告诉编译器这个类由什么字段和方法组成;然后使用这个类的实例化对象。

委托类似,使用一个委托分为两个阶段,首先定义委托,告诉编译器这个委托可以指向哪些类型和方法;然后创建这个委托的实例。

delegate void intMethodInvoker(int x);

定义一个委托要定义方法的参数和返回值。使用关键字delegate。

delegate double TwoLongOp(long first,long second);

delegate string GetAString();

委托将一个方法当作一个参数(变量)来使用;创建对象的过程即创建实例的过程。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 委托
 8 {
 9     class Program
10     {
11         private delegate string GetAString(); //定义了一个委托类型,这个委托类型的名字叫做GetAString
12         static void Main(string[] args)
13         {
14             int x = 40;
15             //string s=x.ToString();
16             //Console.WriteLine(s);
17 
18             //使用委托类型,创建实例
19             GetAString m = new GetAString(x.ToString);  //m指向了x中的ToString方法
20             string a=m();                               //通过委托实例调用x中的ToString方法
21             Console.WriteLine(a);                       //通过委托类型是调用一个方法,跟直接调用这个方法作用是一样的
22             Console.ReadKey();
23 
24 
25         }
26     }
27 }

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 委托
 8 {
 9     class Program
10     {
11         private delegate string GetAString(); //定义了一个委托类型,这个委托类型的名字叫做GetAString
12         static void Main(string[] args)
13         {
14             int x = 40;
15             //string s=x.ToString();
16             //Console.WriteLine(s);
17 
18             //使用委托类型,创建实例
19             //GetAString m = new GetAString(x.ToString);  //m指向了x中的ToString方法
20             GetAString m = x.ToString;
21             //string a=m();                                //通过委托实例调用x中的ToString方法
22             string a = m.Invoke();                        //通过invoke方法调用m所引用的方法
23             Console.WriteLine(a);                        //通过委托类型是调用一个方法,跟直接调用这个方法作用是一样的
24             Console.ReadKey();
25         }
26     }
27 }

实例2 可以把委托类型当做(方法的)参数使用

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 委托
 8 {
 9     class Program
10     {
11         
12         private delegate void PrintString();
13         static void Main(string[] args)
14         {
15             PrintString method = Method1;
16             PrintStr(method);
17             method = Method2;
18             PrintStr(method);
19             Console.ReadKey();
20         }
21 
22         static void PrintStr(PrintString print)
23         {
24             print();
25         }
26 
27         static void Method1()
28         {
29             Console.WriteLine("method01");
30         }
31         static void Method2()
32         {
33             Console.WriteLine("method02");
34         }
35     }
36 }

 

posted @ 2018-05-09 10:41  MR_L先生  阅读(174)  评论(0编辑  收藏  举报