红狐

DBku

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

代码很简单,就不多做解释了

 1 //定义一委托
 2    public delegate int OperationDelegate(int x,int y);
 3     public class Operator
 4     {
 5         private int _x, _y;
 6         public Operator(int x, int y)
 7         {
 8             this._x = x;
 9             this._y = y;
10         }
11 
12         public void Operate(OperationDelegate del)
13         {
14             del(_x, _y);
15         }
16     }

调用

View Code
 1 class Program
 2     {
 3         /// <summary>
 4         /// 加法运算
 5         /// </summary>
 6         /// <param name="x">x</param>
 7         /// <param name="y">y</param>
 8         /// <returns></returns>
 9         private static int Add(int x, int y)
10         {
11             int result = x + y;
12             Console.WriteLine("x + y = {0}",result);
13             return result;
14         }
15 
16         /// <summary>
17         /// 减法运算
18         /// </summary>
19         /// <param name="x">x</param>
20         /// <param name="y">y</param>
21         /// <returns></returns>
22         private static int Sub(int x, int y)
23         {
24             int result = x - y;
25             Console.WriteLine("x - y = {0}", result);
26             return result;
27         }
28 
29         static void Main(string[] args)
30         {
31             //声明一个委托对象
32             OperationDelegate del = null;
33             del += new OperationDelegate(Add);
34             del += new OperationDelegate(Sub);
35 
36             Operator op = new Operator(5, 3);
37             op.Operate(del);
38             //Console.ReadLine();
39 
40             //去掉一个委托实例 
41             del -= new OperationDelegate(Add);
42             op.Operate(del);
43             Console.ReadLine();
44         }
45     }

第一次在博客园里写东西,本人小菜,贴出来的东西只是为了印证学习的过程。

代码出处:

www.codefans.net    具体地址不详细

 

posted on 2013-04-19 13:08  DBku  阅读(157)  评论(0)    收藏  举报