事件与委托的入门例子

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication2 {     #region  事件     public delegate void mydelegate();     /// <summary>     /// 事件处理方法类     /// </summary>     class MyEventHander     {         public void OnHandler1()         {             Console.WriteLine("调用  OnHandler1() 方法!!");         }

    }     /// <summary>     /// 事件声明、触发该事件的方法     /// </summary>     class MyEvent     {         public event mydelegate Event1;         public void FireEvent1()         {             if (Event1 != null)             {                 Event1();             }         }     }     class Program     {         static void Main(string[] args)         {             #region 淡定             //int tagID = 6495;             //byte b1,b2;             //b1 = (byte)(tagID >> 8);             //b2 = (byte)tagID;             //System.Console.WriteLine("b1:{0:X2},b2:{1:X2}", b1, b2);             #endregion

            MyEvent b = new MyEvent();//实例化对象,b为 事件类 对象名             MyEventHander a = new MyEventHander();//实例化对象,a为 事件处理方法类 对象名             b.Event1 += new mydelegate(a.OnHandler1);//将事件处理方法绑定到事件             b.FireEvent1();//事件触发方法         }     }     #endregion     #region 事件         public delegate void mydelegate(int c, int n);//声明一个事件委托         /// <summary>         /// 事件声明、事件触发方法类         /// </summary>         public class Shape         {             protected int color = 0;             public event mydelegate ColorChange;//声明一个事件

            public int pcolor//定义属性             {                 set                 {                     int ocolor = color;                     color = value;                     ColorChange(ocolor, color);                 }                 get { return color; }             }             //public Shape()//构造函数             //{             //    color = 0;             //}         }         /// <summary>         /// 事件处理方法类         /// </summary>         public class MyEventHander         {             public void EentHander(int c, int n)             {                 Console.WriteLine("事件处理方法!");             }         }         class Program         {             static void Main(string[] args)             {                 Shape obj = new Shape();                 MyEventHander obj1 = new MyEventHander();                 obj.ColorChange += new mydelegate(obj1.EentHander);             }

        }     #endregion     #region 委托     delegate void mydelegate(int n);     class MyDeClass     {         public void fun1(int n)         {             Console.WriteLine("{0}的2倍={1}", n, 2 * n);         }         public void fun2(int n)         {             Console.WriteLine("{0}的3倍={1}", n, 3 * n);         }     }     class Program     {         static void Main(string[] args)         {             MyDeClass obj = new MyDeClass();             mydelegate p = new mydelegate(obj.fun1);             p(100);             p = new mydelegate(obj.fun2);             p(2);         }     }     #endregion     #region 委托     delegate double mydelegate(double x, double y);     class MyDeClass     {         public double add(double x, double y)         {             return x + y;         }         public double sub(double x, double y)         {             return x - y;         }         public double mul(double x, double y)         {             return x * y;         }         public double div(double x, double y)         {             return x / y;         }     }     class Program     {         static void Main(string[] args)         {             MyDeClass obj = new MyDeClass();             mydelegate p = obj.add;             Console.WriteLine("5+8={0}", p(5, 8));             p = obj.sub;             Console.WriteLine("5-8={0}", p(5, 8));             p = obj.mul;             Console.WriteLine("5*8={0}", p(5, 8));             p = obj.div;             Console.WriteLine("5/8={0}", p(5, 8));         }

    }     #endregion     #region 委托     delegate void mydelegate(double x, double y);     class MyDeClass     {         public void add(double x, double y)         {             Console.WriteLine("{0}+{1}={2}", x, y, x + y);         }         public void sub(double x, double y)         {             Console.WriteLine("{0}-{1}={2}", x, y, x - y);         }         public void mul(double x, double y)         {             Console.WriteLine("{0}*{1}={2}", x, y, x * y);         }         public void div(double x, double y)         {             Console.WriteLine("{0}/{1}={2}", x, y, x / y);         }     }     class Program     {         static void Main(string[] args)         {             MyDeClass obj = new MyDeClass();             //mydelegate p, a;             //a = obj.add;             //p = a;             //a = obj.sub;             //p += a;             //a = obj.mul;             //p += a;             //a = obj.div;             //p += a;             //p(5, 8);             mydelegate p = new mydelegate(obj.add);             p += obj.div;             p += obj.mul;             p += obj.sub;             p(5, 10);         }     } #endregion     #region 使委托与匿名方法关联     delegate void mydelegate(string mystr);//====没有方法名称的方法,当将委托与匿名方法关联时,直接给出方法的函数体====//     class Program     {         static void Main(string[] args)         {         委托类型名  对象名 返回类型 (参数列表)      匿名方法代码              mydelegate p = delegate(string mystr){Console.WriteLine(mystr);};             p("valeb");             }     }     #endregion }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值