static void Main(string[] args) { SimpleMath simpleMath = new SimpleMath(); SimpleMath.MathMessage mathMessage = new SimpleMath.MathMessage((m, i) => { Console.WriteLine(m); Console.WriteLine("The result is:{0}", i); }); simpleMath.SetMathMessage(mathMessage); simpleMath.MathAdd(2, 2); }
static void Main(string[] args) { #region Anonymous Methods Console.WriteLine("***** Anonymous Methods *****"); int aboutToBlowCounter = 0; // Make a car as usual. Car c1 = new Car("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("Message from Car: {0}", e.msg); }; c1.Exploded += delegate(object sender, CarEventArgs e) { Console.WriteLine("Message from Car: {0}", e.msg); }; // Speed up (this will generate the events.) Console.WriteLine("\n***** Speeding up *****"); for (int i = 0; i < 6; i++) { c1.SpeedUp(20); } Console.WriteLine("AboutToBlow event was fired {0} times", aboutToBlowCounter); #endregion SimpleMath m = new SimpleMath(); m.ComputationFinished += ComputationFinishedHandler; Console.WriteLine("10 + 10 is {0}", m.Add(10, 10)); // .NET 2.0 allows event handlers to be cast into // their underlying delegate. SimpleMath.MathMessage mmDelegate = (SimpleMath.MathMessage)ComputationFinishedHandler; Console.WriteLine(mmDelegate.Method); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Method Conversion *****"); SimpleMath m = new SimpleMath(); m.ComputationFinished += ComputationFinishedHandler; Console.WriteLine("10 + 10 is {0}", m.Add(10, 10)); // Event handlers to be cast into // their underlying delegate. SimpleMath.MathMessage mmDelegate = (SimpleMath.MathMessage)ComputationFinishedHandler; Console.WriteLine(mmDelegate.Method); Console.ReadLine(); }