public delegate void delegate_name(string status);
然后使用 event 关键字来声明事件本身,如下所示:
// 基于上面的委托定义事件
public event delegate_name event_name;
using System; namespace task.lmcjl.com { class Demo { static void Main(string[] args){ PublisherDemo e = new PublisherDemo(); /* 实例发布器类*/ SubscriberDemo v = new SubscriberDemo(); /* 实例订阅器类 */ e.MyEvent += new PublisherDemo.MyEntrust( v.printf ); e.SetValue("C语言中文网"); } } /***********发布器类***********/ public class PublisherDemo{ private string value; public delegate void MyEntrust(string str); public event MyEntrust MyEvent; public void SetValue( string s ){ value = s; MyEvent(value); // 触发事件 } } /***********订阅器类***********/ public class SubscriberDemo{ public void printf(string str){ Console.WriteLine(str); } } }运行结果如下:
C语言中文网
本文链接:http://task.lmcjl.com/news/18347.html