正在阅读:C#事件机制归纳(下)C#事件机制归纳(下)

2004-07-19 10:05 出处:CSDN 作者:changqi 责任编辑:linjixiong

  4.事件的参数的使用。


 

using System;

class Class1

{
      static void Main()
       {
               Student s1=new Student();

               s1.Name ="Student1";

               Student s2=new Student();

               s2.Name ="Student2";

              s1.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

               s2.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

               //当Register方法一执行,触发RegisterOK事件

               //RegisterOK事件一触发,然后执行Student_RegisterOK方法

               s1.Register(); 

              s2.Register();

              Console.ReadLine();
       }
       static void Student_RegisterOK(RegisterOkArgs e)
       {
             Console.WriteLine(e.EventInfo);
       }
}
class Student

{

       public delegate void DelegateRegisterOkEvent(RegisterOkArgs e);

       public event DelegateRegisterOkEvent RegisterOK;

       public string Name;

       public void Register()

       {
              Console.WriteLine("Register Method");
              RegisterOK(new RegisterOkArgs("Student Name: "+Name));
       }
}

class RegisterOkArgs:EventArgs

{
       public string EventInfo;

       public RegisterOkArgs(string eventInfo):base()
       {
              this.EventInfo =eventInfo;
       }
}


察看评论详细内容 我要发表评论
作者笔名 简短内容 发表时间
:
键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品