What is Action, Event and Delegate in simple?

A Delegate is a type which can help passing a function as a parameter. Events help two objects communicate with each other.
  • Events help two objects communicate with each other or be notified when required.
  • Events help in building loosely-coupled applications which are extensible.
   Dopper dopper = new Dopper();
   dopper.DopperDopped += delegate (object o, EventArgs e)
   {
       Console.WriteLine("Dopper Event Handled");
   };
   dopper.DoDope();

   class Dopper
   {
        public event EventHandler<EventArgs> DopperDopped;

        public void DoDope()
        {
            CallRaiser();
        }

        protected virtual void CallRaiser()
        {
            if (DopperDopped != null)
            {
                Console.WriteLine("Raising Event");
                DopperDopped(this, EventArgs.Empty);
            }
        }
    }

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *