コード例 #1
0
ファイル: Delegates.cs プロジェクト: JohnPaine/learning
        /// <summary>
        /// Standard function for tests
        /// </summary>
        /// <exception cref="IOException">I/O exception occured. </exception>
        /// <exception cref="Exception">A delegate callback throws an exception.</exception>
        /// <exception cref="FormatException">Заданная в параметре <paramref name="format" /> спецификация формата недопустима. </exception>
        /// <exception cref="ArgumentNullException">Параметр <paramref name="format" /> имеет значение null. </exception>
        public static void RunTests () {
            try {
                //SimpleExample ();
                //SimpleDelegates ();

                Car car1 = new Car ();
                car1.AboutToBlow += OnCarEngineEvent;
                car1.Exploded += OnCarEngineEvent;
                car1.AboutToBlow +=
                    delegate { Console.WriteLine ("Anonymous method says - you are going too fast!!!"); };
                car1.AboutToBlow +=
                    delegate (object sender, CarEventArgs eventArgs) {
                        Console.WriteLine ("Anonymous method from sender {0} says - {1}", sender.GetType ().Name,
                                           eventArgs.msg);
                    };
                car1.Exploded += (sender, e) => { Console.WriteLine ("Exploded slot with lambda says - " + e.msg); };

                for (int i = 0; i < 11; i++) {
                    car1.Accelerate (10);
                }
            }
            catch (IOException e) {
                Console.WriteLine (e);
            }
        }
コード例 #2
0
ファイル: Delegates.cs プロジェクト: JohnPaine/learning
 private static void SimpleDelegates () {
     Car car1 = new Car ();
     car1.RegisterWithCarEngine (OnCarEngineEvent);
     car1.UnRegisterWithCarEngine (OnCarEngineEvent);
     car1.RegisterWithCarEngine (OnCarEngineEvent);
     for (int i = 0; i < 11; i++) {
         car1.Accelerate (10);
     }
 }