Jacklovely

导航

 

说明(2017-5-29 22:22:50):

1. 语法:public delegate void mydel();这一句在类外面,命名空间里面。

2. 专门新建一个方法,参数是委托:

public static void test(mydel mdl)
{
  mdl();
}

3. 在main函数里,调用这个方法,参数是要使用的方法:

test(show);

4. 感觉test这个方法只是一个中转站,里面存放委托和参数。

例1:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _04委托
 8 {
 9     public delegate void mydel();
10     class Program
11     {
12        
13         static void Main(string[] args)
14         {
15             test(show);
16             Console.ReadKey();
17         }
18         public static void test(mydel mdl)
19         {
20             mdl();
21         }
22         public static void show()
23         {
24             Console.WriteLine("吃饭了!");
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 _04委托
 8 {
 9     public delegate int mydel(int x, int y);
10     class Program
11     {
12 
13         static void Main(string[] args)
14         {
15             test(add, 1, 2);
16             Console.ReadKey();
17         }
18         public static void test(mydel mdl, int x, int y)
19         {
20             Console.WriteLine(mdl(x, y));
21         }
22         public static int add(int x, int y)
23         {
24             return x + y;
25         }
26     }
27 }

 

posted on 2017-05-29 22:33  Jacklovely  阅读(249)  评论(0编辑  收藏  举报