static void Main(string[] args) { Console.WriteLine("EventsSample!"); //创建一个保护事件的实例 var dealer = new CarDealer(); var daniel = new Consumer("Daniel"); //注册 事件daniel.NewCarIsHere dealer.NewCarInfo += daniel.NewCarIsHere; //触发事件 回调daniel.NewCarIsHere; dealer.NewCar("Mercedes"); var sebastian = new Consumer("Sebastian"); //注册 sebastian.NewCarIsHere dealer.NewCarInfo += sebastian.NewCarIsHere; //触发事件 回调daniel.NewCarIsHere; sebastian.NewCarIsHere; dealer.NewCar("Ferrari"); dealer.NewCarInfo -= sebastian.NewCarIsHere; dealer.NewCar("Red Bull Racing"); ReadKey(); }
public static void Main(string[] args) { var dealer = new CarDealer(); var valtteri = new Consumer("Valtteri"); dealer.NewCarInfo += valtteri.NewCarIsHere; dealer.NewCar("Williams"); var max = new Consumer("Max"); dealer.NewCarInfo += max.NewCarIsHere; dealer.NewCar("Mercedes"); dealer.NewCarInfo -= valtteri.NewCarIsHere; dealer.NewCar("Ferrari"); }
static void Main(string[] args) { var dealer = new CarDealer(); var michael = new Consumer("Michael"); dealer.NewCarInfo += michael.NewCarIsHere;//订阅事件 dealer.NewCar("Ferrari"); var nick = new Consumer("Sebastian"); dealer.NewCarInfo += nick.NewCarIsHere; dealer.NewCar("Mercedes"); dealer.NewCarInfo -= michael.NewCarIsHere;//注销事件 dealer.NewCar("Red Bull Racing"); Console.ReadKey(); }
static void Main() { var publisher = new CarDealer(); var michaelConsumer = new Consumer("Michael"); publisher.NewCarInfo += michaelConsumer.Notify; publisher.NewCar("Ferrari"); var nickConsumer = new Consumer("Sebastian"); publisher.NewCarInfo += nickConsumer.Notify; publisher.NewCar("Mersedes"); publisher.NewCarInfo -= michaelConsumer.Notify; publisher.NewCar("Red Bull Racing"); Console.ReadLine(); }