コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Delegates as event enablers ******\n");

            // First, make a Car object.
            Car c1 = new Car("Slugbug", 100, 10);

            // Now, tell the car which method to call when it wants to send us messages.
            c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));

            // This time, hold onto the delegate object, so we can unregister later.
            Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
            c1.RegisterWithCarEngine(handler2);

            // Speed up (this will trigger the events).
            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();

            // Unregister from the second handler.
            c1.unRegisterWithCarEngine(handler2);

            c1.CurrentSpeed = 30;

            Console.WriteLine("***** Speeding up *****");
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }
            Console.ReadLine();
        }