static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); Car c1 = new Car(); // 注册简单的方法名称 c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } // 注销简单的方法名称 c1.UnRegisterWithCarEngine(CallMeHere); // 没有通知 for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); // note we are not directly allocatin the associated delegate obj, but rather simply specifying a method that matatch // the delegate's expected signature. Car c1 = new Car(); // old way of register //c1.RegisterWithCarEngine(new Car.CarEngineHandler(CallMeHere)); // Register the simple method name. c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } // Unregister the simple method name. c1.UnRegisterWithCarEngine(CallMeHere); // No more notifications! for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); Car c1 = new Car(); // register the simple method name c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding Up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } // un-register the simple method name c1.UnRegisterWithCarEngine(CallMeHere); // no more notifications! for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.WriteLine("Press <Enter> to exit. . ."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); Car myCar = new Car(); myCar.MaxSpeed = 80; // set the max speed Console.WriteLine($"MaxSpeed is {myCar.MaxSpeed}"); // Register the simple method name. Note it has to correspond to // delegate signature. myCar.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { myCar.Accelerate(20); } // Unregister the simple method name. myCar.UnRegisterWithCarEngine(CallMeHere); Console.WriteLine("Unregister CallMeHere, message from handler now stops"); // No more notifications! for (int i = 0; i < 6; i++) { myCar.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); Car c1 = new Car(); // Регистрируем имя метода. c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } // Отменяем регистрацию метода. c1.UnRegisterWithCarEngine(CallMeHere); // Уведомлений не поступает! for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); Car c1 = new Car(); // register the simple method name c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } // unregister the simple method name c1.UnRegisterWithCarEngine(CallMeHere); // unregistered properly? for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Car c1 = new Car(); c1.RegisterWithCarEngine(CallMeHere); c1.RegisterWithCarEngine(CallMeHere); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } c1.UnRegisterWithCarEngine(CallMeHere); //works fine, unregistered one //c1.UnRegisterWithCarEngine(CallMeHere); //works fine for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); Car c1 = new Car(); // Register the simple method name. c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } // Unregister the simple method name. c1.UnRegisterWithCarEngine(CallMeHere); // No more notifications! for (int i = 0; i < 6; i++) { c1.Accelerate(20); } }
static void Main(string[] args) { Console.WriteLine("******Method Group Conversion******"); Car c1 = new Car("sliy", 100, 10); c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("*********speed up!*************"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } c1.UnRegisterWithCarEngine(CallMeHere); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("****i Method Group Conversion *****\n"); Car c1 = new Car(); // зарегистрировать простое имя метода c1.RegisterWithCarEngine(CallMeWhere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } // Отменить регистрацию простого имени метода c1.UnRegisterWithCarEngine(CallMeWhere); // Уведомления больше не поступают! for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main( string[] args ) { Console.WriteLine("***** Method Group Conversion *****\n"); Car c1 = new Car(); // Register the simple method name. c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) c1.Accelerate(20); // Unregister the simple method name. c1.UnRegisterWithCarEngine(CallMeHere); // No more notifications! for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("****Method Group Conversion *****\n"); Car car = new Car(); car.RegisterWithCarEngine(CallMeHere); // Увеличить скорость (это инициирует события). Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { car.Accelerate(20); } //Уведомления больше не поступают car.UnRegisterWithCarEngine(CallMeHere); for (int i = 0; i < 6; i++) { car.Accelerate(20); } Console.ReadLine(); }
static void Group() { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("=> Method Group Conversion"); Car c1 = new Car("SlugBug", 100, 10); c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("===== Speeding up ====="); for (int i = 0; i < 7; i++) { c1.Accelerate(20); } c1.UnRegisterWithCarEngine(CallMeHere); c1.Speed = 10; c1.StatusReset(); Console.WriteLine("----- Speeding up -----"); for (int i = 0; i < 7; i++) { c1.Accelerate(20); } }
private static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); var c1 = new Car(); c1.RegisterWithCarEngine(new Car.CarEngineHandler(CallMeHere)); Console.WriteLine("***** Speeding up *****:"); for (var i = 0; i < 6; i++) { c1.Accelerate(20); } c1.UnRegisterWithCarEngine(CallMeHere); for (var i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Delegates as event enablers *****\n"); Car c1 = new Car("SlugBug", 100, 10); c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("\n***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } c1.UnRegisterWithCarEngine(CallMeHere); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Group Conversion *****\n"); Car c1 = new Car(); //方法组转换,调用以委托作为参数的方法时直接提供方法的名称,而不用创建委托对象 c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speed Up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } c1.UnRegisterWithCarEngine(CallMeHere); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }