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(); 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(); c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("***** Speeding up ******"); for (int i = 0; i < 6; i++) { c1.Accelerat(20); } c1.UnRegisterWithCarEngine(CallMeHere); for (int i = 0; i < 6; i++) { c1.Accelerat(20); } }
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) { Car c1 = new Car(); //register a simple method name c1.RegisterWithCarEngine(CallMeHere); Console.WriteLine("Vroom vroom"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } //unregister the method c1.UnrgeisterWithCarEngine(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 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(); }