public static void StartIt() { TrainSignal ts = new TrainSignal(); Car car1 = new Car(ts); Car car2 = new Car(ts); Car car3 = new Car(ts); Car car4 = new Car(ts); ts.TrainIsComing(); Console.WriteLine("Set the delegate to NULL."); ts.OnTrainSignal1 = null; ts.OnTrainSignal2 = null; // ts.OnTrainSignal3 = null; // this is not possible on an event // ts.OnTrainSignal4 = null; // this bad ts.TrainIsComing(); // ts.OnTrainSignal1(); -- Crash // ts.OnTrainSignal2(); -- Crash // ts.OnTrainSignal3(); // can't call the event handler directly // ts.OnTrainSignal4(); // can't call the event handler directly }
public Car(TrainSignal trainsignal) { trainsignal.OnTrainSignal1 += StopTheCar; trainsignal.OnTrainSignal2 += StopTheCar; trainsignal.OnTrainSignal3 += StopTheCar; trainsignal.OnTrainSignal4 += StopTheCarArgs; }