Explain what is an Action, Event and Delegate.

  • A Delegate is a type which can help passing a function as a parameter - similar to pointer functions.

What are Delegates? How do you use them?

  • 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);
            }
        }
    }
  • Action is a special type of Delegate that represents a method with generic arguments and no return type.

What is the difference between Func and Action delegates in C#?

C# Concepts Posted May 14, 2021

You can now show your support. 😊

We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept