1

I was asked this question in an interview, and either I'm suffering from brain-lock or just plain dumb, but I didn't have an answer.

VillageTech
  • 1,968
  • 8
  • 18
grv_9098
  • 465
  • 5
  • 16
  • 1
    If I remember correctly, events are implemented using Multicast delegates. However, the delegate is usually private (so that consumers can't play on it directly). Events on the other hand have a public API – Flydog57 Dec 17 '19 at 20:00
  • Per [msdn](https://learn.microsoft.com/en-us/dotnet/api/system.multicastdelegate?view=netframework-4.8) _Compilers and other tools can derive from this class, but you cannot derive from it explicitly. The same is true of the `Delegate` class._ – Pavel Anikhouski Dec 17 '19 at 20:10
  • 1
    Events are used to *restrict* access to a delegate. Just like properties restrict access to a regular variable. Properties permit get and set, events permit add and remove. Important, this ensures that external code cannot make event subscriptions from other code disappear with a single assignment. They mostly look different because you don't often actually write the add and remove accessors. The ones that the compiler auto-generates are often good enough. – Hans Passant Dec 17 '19 at 20:12
  • you're not the only one https://stackoverflow.com/questions/563549/difference-between-events-and-delegates-and-its-respective-applications – Nikki9696 Dec 17 '19 at 20:30
  • also see https://learn.microsoft.com/en-us/dotnet/csharp/distinguish-delegates-events – Nikki9696 Dec 17 '19 at 20:34
  • Can you answer the question "since we have fields, why do we need properties?" If you can answer that question, you can make a good start on the question you asked. If you cannot answer that question, work on that one first! – Eric Lippert Dec 17 '19 at 21:40
  • Hi Grv_9098, did the answer help answer your question. If yes please consider accepting it by clicking the check-mark. This will also help others understand you've found a solution – Clint Dec 19 '19 at 13:34

1 Answers1

0

Couple of reasons why we need events:

  • Restricting scope, you do not want to expose your events, like you can for your delegates.
  • You can have events as fields in your interfaces and not delegates

Example below:

Class Printer
{
    public event EventHandler Print;

    public void Start()
    {
        OnPrint();
    }

    protected virtual void OnPrint()
    {
        Print?.Invoke(this,EventArgs.Empty);
    }
}
class Program
{
    static void Main(string[] args)
    {
        //When Print is an EventHander
        var printer = new Printer();
        printer.Print += PrintEvent;
        printer.Start();

        //If Print was a delegate this is possible, else you get compile time errors 
        printer.Print(null,null); // Events will not allow to have a direct invoke 
        printer.Print = null; //You cannot assign a null to an Event Handler
    }
    private static void PrintEvent(object sender, EventArgs e)
    {
        System.Console.WriteLine("Printing event");
    }
}
Clint
  • 6,011
  • 1
  • 21
  • 28