Exemple #1
0
        static void Main(string[] args)
        {
            Car car = new Car("Tesla");

            Car.AboutToBlow aTB = new Car.AboutToBlow(car.aboutToBlow);
            Car.Exploded    exp = new Car.Exploded(car.exploded);

            car.onAboutToBlow(aTB);
            car.onExploded(exp);

            Console.WriteLine("*****Speeding Up*******");

            try
            {
                for (int i = 0; i < 10; i++)
                {
                    car.accelerate(20);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: Car is dead already!");
            }


            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers *****");

            // Make a car as usual. 
            Car c1 = new Car("SlugBug", 100, 10);

            // Register event handlers.          
            Car.Exploded d = new Car.Exploded(CarExploded);
            c1.OnAboutToBlow(new Car.AboutToBlow(CarIsAlmostDoomed));
            c1.OnAboutToBlow(new Car.AboutToBlow(CarAboutToBlow));
            c1.OnExploded(d);

            // Speed up (this will generate the events.)
            Console.WriteLine("\n***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.SpeedUp(20);

            // Remove CarExploded method 
            // from invocation list.
            c1.RemoveExploded(d);

            Console.WriteLine("\n***** Speeding up *****");
            for (int i = 0; i < 6; i++)
                c1.SpeedUp(20);

            Console.ReadLine();
        }