public void Play() { Console.WriteLine("PlayingMovie"); Thread.Sleep(3000); //you fire an event simply by calling it like a method //if(MovieEnd != null) //{ // MovieEnd(); //} MovieEnd?.Invoke(Currentmovie.Title); //that will run all methods that are subscribed to the event //events with no subcribers are null, and cause null exception when fired //so we need to check against null first //in c#, we have a couple of null handling operators //?, is the null-conditional operator //it means, behave like . if the thing to the left is not null //otherwise do nothing at all //otherwise do nothing at all }
public void Play() { Console.WriteLine("Playing movie."); Thread.Sleep(3000); // movie plays for 3 seconds // you fire an event simply by calling it like a method //MovieEnd?.Invoke(); MovieEnd?.Invoke(CurrentMovie.Title); // pass parameters to all subscribers // that will run all methods that are subscribed to the event. // events with no subscribers are null, and cause null exception when fired. // so we need to check against null first. // in C#, we have a couple of null-handling operators. // ?. is the null-conditional operator. // it means, behave like . if the thing to the left is not null. // otherwise, do nothing at all. //if (a != null) //{ // if (a.b != null) // { // a.b.operate(); // } //} //a.b.operate(); }