static void Main(string[] args) { Console.WriteLine("***** Fun with Events *****\n"); Car c1 = new Car("SlugBug", 100, 10); // Register event handleres. c1.AboutToBlow += CarIsAlmostDoomed; c1.AboutToBlow += CarAboutToBlow; c1.Exploded += CarExpleded; Console.WriteLine("***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); // Remove Car Exploded method // from invocation list. c1.Exploded -= CarExpleded; } Console.WriteLine("\n****** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Events *****\n"); Car c1 = new Car("Slug Bug", 100, 10); //register event handlers c1.AboutToBlow += new Car.CarEngineHandler(CarIsAlmostDoomed); c1.AboutToBlow += new Car.CarEngineHandler(CarAboutToBlow); Car.CarEngineHandler d = new Car.CarEngineHandler(CarExploded); c1.Exploded += d; Console.WriteLine("***** Speeding Up *****\n"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } //remove CarExploded method from the invocation list c1.Exploded -= d; Console.WriteLine("***** Speeding Up *****\n"); for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** More Fun with Lambdas *****\n"); // Make a car as usual. Car c1 = new Car("SlugBug", 100, 10, 50); // Now with lambdas! c1.AboutToBlow += (sender, e) => { Console.WriteLine(e.msg); }; c1.Exploded += (sender, e) => { Console.WriteLine(e.msg); }; c1.SlowedDown += (sender, e) => { Console.WriteLine(e.msg); }; // Speed up (this will generate the events). Console.WriteLine("\n***** Speeding up *****"); for (int i = 0; i < 6; i++) { if (c1.CurrentSpeed >= 90) { c1.CurrentSpeed = 0; } else { c1.Accelerate(20); } } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Events *****\n"); Car c1 = new Car("Slug Bug", 100, 10); c1.AboutToBlow += (sender, e) => Console.WriteLine(e.msg); //c1.AboutToBlow += CarAboutToBlow; c1.Exploded += (sender, e) => Console.WriteLine(e.msg); Console.WriteLine("***** Speeding Up *****"); for (int i = 0; i < 6; i++) c1.Accelerate(20); Console.ReadLine(); }
static void Main(string[] args) { Car c1 = new Car("SlugBug", 100, 10); c1.AboutToBlow += CarIsAlmostDoomed; // c1.AboutToBlow += new Car.CarEngineHandler(CarAboutToBlow); c1.AboutToBlow += CarAboutToBlow; //method group conversion works fine EventHandler<CarEventArgs> d = CarExploded; c1.Exploded += d; for (int i = 0; i < 6; i++) { c1.Accelerate(20); } c1.Exploded -= d; for (int i = 0; i < 6; i++) { c1.Accelerate(20); } Console.ReadLine(); }
public static void HookIntoEvents() { Car newCar = new Car(); newCar.AboutToBlow += newCar_AboutToBlow; }