- using System;
- class recv
- {
- public delegate void request(object sender,EventArgs e);//定义了一个函数类型。
- public event request rqadd;
- //事件其实就是一个函数对象,而这个函数的调用方式有点特别,是事件驱动的,而不是直接调用的。
- //而且这个函数可以认为是一个reqeust类型的函数组,这个函数组目前为空。
- public void run()//需要一个循环来持续的处理事件。
- {
- while(true)
- {
- if(Console.ReadLine()=="h")
- {
- rqadd(this,new EventArgs());
- //执行事件函数数组里的所有函数。
- }
- }
- }
- }
- class client
- {
- public client(recv rq)
- {
- rq.rqadd += new recv.request(this.show);//把show函数添加到rq的事件数组当中。
- }
- private void show(object sender,EventArgs e)
- {
- Console.WriteLine("h is received!");
- }
- }
- class myclass
- {
- static void Main()
- {
- recv rq=new recv();
- client cc=new client(rq);
- rq.run();
- }
- }