// technically we can even subscribe to delegate-typed members without events // at all but it is more limited // return true if success, false if failure public bool Play() { Console.WriteLine("Playing Movie."); // wait for 3 seconds Thread.Sleep(3000); // firing the MovieFinished event // null-check because if there are zero subscribers, // that is a null reference exception //if (MovieFinished != null) //{ // MovieFinished(); //} MovieFinished?.Invoke(); // this will call not just one function, but all event subscribers. // ?. is a special operator called the null-coalescing operator // If the thing to the left is null, it simply returns null. // if the thing to the left is NOT null, it will behave like . (dot) // ?? is the null-coalescing operator // a ?? b, that means, return a, unless it's null, in which case return b. // When we fire the event, we provide all parrameters that the event // says its handlers needs DiscEjected?.Invoke(CurrentMovie.ToString()); return(true); }
// technically we can even subscribe to delegate typed members without events at all // but it's a bit more limited // return true if success, false if failure public bool Play() { // wait 3 secs Thread.Sleep(3000); // firing the Moviefinished event // must null check because there are 0 subscribers // nullReferenceException //if (MovieFinished is null) //{ // MovieFinished(); //} // firing the MovieFinished Event MovieFinished?.Invoke(); // will not just call one fn, but all event subscribers // `?.` is a special operator called the null coalescing operator // if the thing to left is null, it returns null // if thing to the left is not null, will behave like . // `??` is the null coalescing operator // a ?? b, means return a, unless it's null, in which case returns b // when we fire the event, we provide all the params that the event says it needs DiscEjected?.Invoke(CurrentMovie.ToString()); return(true); }
//technically we can even subscribe to delegate-typed members // without events at all, but it's a bit more limited //return true if success, false if failure public bool Play() { //wait for 3 seconds Thread.Sleep(3000); //firing the MovieFinished event //must null-check because if there are zero subscribers, // that's a nullreferenceexception //if (MovieFinished != null) //{ // if (MovieFinished != null) // MovieFinished(); //} MovieFinished?.Invoke(); //this will call not just one function but all event subscribers //?. is a special operator called the null-coalescing operator //if the thing to the left of it is null, it simply returns null //if the thing to the left is NOT null, it will behave like .(dot) //?? is the null-coalescing operator //a ?? b, means return a unless it's null, in which case return b //when we fire the event, we provide all parameters that the event // says its handlers need DiscEjected?.Invoke(CurrentMovie.ToString()); return(false); }
// technically we can subscribe to delegate-typed members without events at all // but it's a bit more limited //return true if success, false if failure public bool Play() { //wait for 3 seconds Thread.Sleep(3000); // Firing the MovieFinished event // MovieFinished(); // this will call not just one function, but all event subscribers. MovieFinished?.Invoke(); // ?. is a special operator called the null-coalescing operator // if the thing to the left of it is null, it simply returns null. // if the thing to the left is not null, it will behave like . // when we fire the event, we provide all parameters that the event // needs DiscEjected?.Invoke(CurrentMovie.ToString()); return(true); }
public bool Play() { Thread.Sleep(3000); // fire event //if (MovieFinished != null) //{ // MovieFinished(); //} // ?. is a special operator called the null-coalescing operator // if the thing to the left of it is null, it simply returns null. // if the thing to the left is not null, it will behave like . // ?? is the null-coalescing operator // a ?? b, that means, return a, unless it's null, in which case return b. MovieFinished?.Invoke(); // this calls all event subscribers DiscEjected?.Invoke(CurrentMovie.ToString()); return(true); }