Пример #1
0
        // the event is declared as having a particular kind of event handler
        // method that can be subscribed to it with: the PlayFinishedHandler.

        // "Action<string>" represents, a void-return function with 1 string param.

        public void Play()
        {
            //Console.WriteLine("Playing inserted movie " + CurrentMovie.Name);

            // string interpolation syntax
            // start the string with $, and you can use {} to switch "back"
            // to C# code.
            Console.WriteLine($"Playing inserted movie {CurrentMovie.Name}");

            Thread.Sleep(3000); // wait for 3 seconds

            // then, we fire the event.
            //PlayFinished();
            // this looks the same as a method call, but it is an event.
            // it results in, all the subscribed functions being called.

            // if there are no subscribers, firing the event is a null exception.
            // so, we never fire an event just like that, instead, we check for null.
            //if (PlayFinished != null)
            //{
            //    PlayFinished();
            //}
            // must provide arguments according to the parameters on that delegate type.
            PlayFinished?.Invoke(CurrentMovie.Name);
            // it turns out that events have an "Invoke" method on them
            // which does the same as what we wrote before

            // there is a null-conditional operator "?." which is pretty cool...
            // if the thing before the ?. is null, then overall it returns null / does nothing.
            // otherwise, it will do member access like "." usually does.

            //if (obj != null)
            //{
            //    if (obj.prop != null)
            //    {
            //        if (obj.prop.asdf != null)
            //        {
            //            // use obj.prop.asdf
            //        }
            //    }
            //}

            //obj?.prop?.asdf?.whatever

            // there's another one called null-coalescing operator
            // ??
        }
Пример #2
0
 void ISoundStopEventReceiver.OnSoundStopped(ISound sound, StopEventCause reason, object userData)
 {
     _soundPlaying = null;
     PlayFinished?.Invoke(_audioClip);
 }
Пример #3
0
 private void SendStopEventToListeners()
 {
     PlayFinished?.Invoke(_audioClip);
 }