public static void TestLocalVariables()
        {
            Console.WriteLine("***** Anonymous Methods *****\n");
            int aboutToBlowCounter = 0;

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

            // Register event handlers as anonymous methods.
            c1.AboutToBlow += delegate
            {
                aboutToBlowCounter++;
                Console.WriteLine("Eek! Going too fast!");
            };

            c1.AboutToBlow += delegate(object sender, CarEventArgs e)
            {
                aboutToBlowCounter++;
                Console.WriteLine("Critical Message from Car: {0}", e.msg);
            };

            // This will eventually trigger the events.
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.WriteLine("AboutToBlow event was fired {0} times.",
                              aboutToBlowCounter);
            Console.ReadLine();
        }
        /* It is possible to associate an event directly to a block of code statements at the time of event registration. Formally, such code
         * is termed an anonymous method
         */
        public static void TestEventsWithAnonymousMethods()
        {
            Console.WriteLine("***** Anonymous Methods *****\n");
            CarEventArgsTest c1 = new CarEventArgsTest("SlugBug", 100, 10);

            // Register event handlers as anonymous methods.
            c1.AboutToBlow += delegate
            {
                Console.WriteLine("Eek! Going too fast!");
            };

            c1.AboutToBlow += delegate(object sender, CarEventArgs e)
            {
                Console.WriteLine("Message from Car: {0}", e.msg);
            };

            c1.Exploded += delegate(object sender, CarEventArgs e)
            {
                Console.WriteLine("Fatal Message from Car: {0}", e.msg);
            };

            // This will eventually trigger the events.
            for (int i = 0; i < 6; i++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }
Esempio n. 3
0
            public static void TestEventArgs()
            {
                Console.WriteLine("***** Fun with Events *****\n");
                CarEventArgsTest c1 = new CarEventArgsTest("SlugBug", 100, 10);

                // Register event handlers.
                c1.AboutToBlow += CarAboutToBlow;
                c1.Exploded    += CarExploded;

                Console.WriteLine("***** Speeding up *****");
                for (int i = 0; i < 6; i++)
                {
                    c1.Accelerate(20);
                }
                Console.ReadLine();
            }
        public static void RetrofitCarEventsWithLambdas()
        {
            Console.WriteLine("***** More Fun with Lambdas *****\n");

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

            // Hook into events with lambdas!
            c1.AboutToBlow += (sender, e) => { Console.WriteLine(e.msg); };
            c1.Exploded    += (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++)
            {
                c1.Accelerate(20);
            }

            Console.ReadLine();
        }