码迷,mamicode.com
首页 > Windows程序 > 详细

C#学习笔记(7)——委托

时间:2017-05-29 22:57:14      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:delegate   语法   ati   reading   ext   namespace   span   main函数   string   

说明(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 }

 

C#学习笔记(7)——委托

标签:delegate   语法   ati   reading   ext   namespace   span   main函数   string   

原文地址:http://www.cnblogs.com/Jacklovely/p/6919224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!