// Event handler public void HandleWhenExceedSpeed(Car source, CarExceedSpeedEventArgs e) { System.Console.WriteLine("{1} : You are driving at {0}. Slow down!", source.Speed, e.WhenViolate); }
static void Main(string[] args) { Car car = new Car(); HighwayPatrol highwayPolice = new HighwayPatrol(); // Wiring the event handler of the listener to the event car.CarSpeedChanged += highwayPolice.HandleWhenExceedSpeed; car.Speed = 200; // Raise the event System.Threading.Thread.Sleep(1000); // Wait for 1 second car.Speed = 300; // Raise the event again System.Console.ReadKey(); }