C#委托事件隨筆
阿新 • • 發佈:2017-11-07
pan eve ogr 隨筆 lin 註冊事件 dev obj event
//用戶去銀行取錢(發布方),用戶收到短信提醒,(接收方) class Program { static void Main(string[] args) { Bank b = new Bank(); //實例化銀行 User xm = new User("110","[email protected]"); //實例化用戶 b.send += new Bank.sendEventHandle(xm.ReviceTel); //註冊事件 b.send+= new Bank.sendEventHandle(xm.ReviceEmail); //註冊事件 b.issure(xm); //執行事件 Console.ReadKey(); } } public class Bank { public delegate void sendEventHandle(object o, User xx); //定義委托 public event sendEventHandle send; //基於委托的事件public void issure(User xx) { send(this,xx); } } public class User:EventArgs{ public string tel; public string email; public User(string telephone, string email) { this.email = email; this.tel = telephone; }public void ReviceTel(object o , User x) { Console.WriteLine("收到短信"+ x.tel); } public void ReviceEmail(object o, User x) { Console.WriteLine("收到emile" + x.tel); } }
C#委托事件隨筆