<repo_name>SalomonFrux/EventsAndDelegates<file_sep>/VideoUploader.cs
﻿using System;
using System.Threading;

namespace EventsAndDelegates
{
    public class VideoUploadedEventArgs
    {
        public Video Video { get; set; }
    }
    public class VideoUploader
    {
       // public delegate void VideoUploaderEventHandler(object source, EventArgs args);

        public event EventHandler<VideoUploadedEventArgs> VideoUploaded;

        public void Encode(Video video)
        {
            if (string.IsNullOrWhiteSpace(video.VideoTitle))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(" No video was uploaded");
            }
            //The encoding logic would not be implemented for now
            else
            {


                Console.WriteLine("The video is being Uploaded...");

                Thread.Sleep(3000);
                
                //We want to notify the subscribers anytime a video is uploaded: that where we use events

                OnVideoUploaded(video);
            }

        }

        protected void OnVideoUploaded(Video video)
        {
            VideoUploaded?.Invoke(this, new VideoUploadedEventArgs(){Video = video} );
        }
    }
}<file_sep>/README.md
# EventsAndDelegates
Helped me get a better understanding of this concept which help extend and application without breaking it in a real world example
<file_sep>/SmsSenderOnVideoUploaded.cs
﻿using System;

namespace EventsAndDelegates
{
    public class SmsSenderOnVideoUploaded
    {
        public void SendSmsOnVideoUploaded(object source, VideoUploadedEventArgs eventArgs)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Video: "+eventArgs.Video.VideoTitle +" uploaded, sending an sms to the uploader...");
        }
    }
}<file_sep>/Program.cs
﻿using System;

namespace EventsAndDelegates
{
    class Program
    {
        static void Main(string[] args)
        {
            var video = new Video(){VideoTitle = "<NAME>"};
            var  uploader = new VideoUploader();
            var smsSender = new SmsSenderOnVideoUploaded();

            var mailSender = new MailSender();
            uploader.VideoUploaded += mailSender.SendMailOnVideoUploaded;
            uploader.VideoUploaded += smsSender.SendSmsOnVideoUploaded;
            uploader.Encode(video);
            Console.Read();
        }
    }
}
<file_sep>/MailSender.cs
﻿using System;

namespace EventsAndDelegates
{
    public class MailSender
    {
        public void SendMailOnVideoUploaded(object source, VideoUploadedEventArgs eventArgs)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Video: "+eventArgs.Video.VideoTitle + " uploaded sending an Email to the uploader...");
        }
    }
}