public event CatShoutEventHandler CatShout; //宣告事件CatShout,事件類型是委託CatShoutEventHandler public void Shout() { Console.WriteLine("喵,我是{0}", name); if (CatShout != null) { CatShoutEventArgs e = new CatShoutEventArgs(); //給Name賦予值 e.Name = this.name; CatShout(this, e); //如果當執行shout()方法時,如果catshout有物件登記事件,則執行CatShout() //object sender就是傳遞發送通知的物件,EventArgs是包含事件數據的類別 } }
public void Run(object sender,CatShoutEventArgs args) { Console.WriteLine("{0}老貓來,{1}",args.Name, name); }
public void Shout() { Console.WriteLine("喵,我是{0}", name); if (CatShout != null) { CatShoutEventArgs e = new CatShoutEventArgs(); //給Name賦予值 e.Name = this.name; CatShout(this,e); //如果當執行shout()方法時,如果catshout有物件登記事件,則執行CatShout() //object sender就是傳遞發送通知的物件,EventArgs是包含事件數據的類別 } }
public void Run(object sender, CatShoutEventArgs args) { Console.WriteLine("{0}老貓來,{1}", args.Name, name); }