SoFunction
Updated on 2025-03-07

Analysis of the difference between delegate and event in C#

This article analyzes the difference between commission and event in C# and shares it with you for your reference. The details are as follows:

Generally speaking, a delegate is a class that maintains a field inside it, pointing to a method. An event can be regarded as a delegate type variable, registering, canceling multiple delegates or methods through an event. This article uses multiple methods of delegation and event execution to understand the difference between the two.

1. Through the entrustment execution method

class Program
{
    static void Main(string[] args)
    {
      Example example = new Example();
      ();
      ();
    }
}
public class Example
{
    public delegate void DoSth(string str);
    internal void Go()
    {
      //Declare a delegate variable and take a known method as a parameter to its constructor      DoSth d = new DoSth(Print);
      string str = "Hello,World";
      //Trigger the delegate through the static method Invoke      (str);
    }
    void Print(string str)
    {
      (str);
    }
}

The above code implementation:

① When CLR is running, the delegate DoSth is actually a class. This class has a constructor with a parameter type of method and provides an Invoke instance method to trigger the execution of the delegate.
② Delegate DoSth to define the parameters and return types of the method
③ By delegating the DoSth constructor, the method that meets the definition can be assigned to the delegate.
④ Call the delegated instance method Invoke executes the method

But in fact, there is another way to get the delegate execution method, that is: delegate variable (parameter list)

public class Example
{
    public delegate void DoSth(object sender, EventArgs e);
    internal void Go()
    {
      //Declare a delegate variable and take a known method as a parameter to its constructor      DoSth d = new DoSth(Print);
      object sender = 10;
      EventArgs e = new EventArgs();
      d(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      (sender);
    }
}

The above code implementation:

① The parameter list of the delegated DoSth and the parameter list of the method Print are still consistent
② The parameter object sender in the delegation DoSth is usually used to represent the initiator of the action, and EventArgs e is used to represent the parameters brought by the action.

In fact, delegate variables (parameter lists), events are executed in this form.

2. Through event execution method

public class Example
{
    public delegate void DoSth(object sender, EventArgs e);
    public event DoSth myDoSth;
    internal void Go()
    {
      //Declare a delegate variable and take a known method as a parameter to its constructor      DoSth d = new DoSth(Print);
      object sender = 10;
      EventArgs e = new EventArgs();
      myDoSth += new DoSth(d);
      myDoSth(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      (sender);
    }
}

The above code implementation:

① Declare the event myDoSth, the type of event is DoSth, the delegation
② Register a delegation for the event by +=
③ Register delegate instances for event through the constructor of DoSth delegate
④ Use the delegate variable (parameter list) form to allow event execution method

Moreover, multiple delegations can be registered for events through +=.

public class Example
{
    public delegate void DoSth(object sender, EventArgs e);
    public event DoSth myDoSth;
    internal void Go()
    {
      //Declare a delegate variable and take a known method as a parameter to its constructor      DoSth d = new DoSth(Print);
      DoSth d1 = new DoSth(Say);
      object sender = 10;
      EventArgs e = new EventArgs();
      //Register multiple delegations for the event      myDoSth += new DoSth(d);
      myDoSth += new DoSth(d1);
      myDoSth(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      (sender);
    }
    void Say(object sender, EventArgs e)
    {
      (sender);
    }
}

As mentioned above, register 1 or more delegated instances for events by +=. In fact, you can also register methods directly for events.

public class Example
{
    public delegate void DoSth(object sender, EventArgs e);
    public event DoSth myDoSth;
    internal void Go()
    {
      object sender = 10;
      EventArgs e = new EventArgs();
      //Register multiple delegations for the event      myDoSth += Print;
      myDoSth += Say;
      myDoSth(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      (sender);
    }
    void Say(object sender, EventArgs e)
    {
      (sender);
    }
}  
 

3. Execute method through EventHandler

Let’s first look at the source code of EventHandler.

public delegate void EventHandler(object sender,  e);

It can be seen that EventHandler is the delegation. Now use EventHandler to execute multiple methods.

public class Example
{
    public event EventHandler myEvent;
    internal void Go()
    {
      object sender = 10;
      EventArgs e = new EventArgs();
      //Register multiple delegations for the event      myEvent += Print;
      myEvent += Say;
      myEvent(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      (sender);
    }
    void Say(object sender, EventArgs e)
    {
      (sender);
    }
}

Summarize:

① Delegation is a class, which can also be instantiated. The method is assigned to the delegate instance through the delegate constructor.
② There are two ways to trigger the delegation: Delegate instance.Invoke (parameter list), Delegate instance (parameter list)
③ Events can be regarded as a delegate variable
④ Register multiple delegate instances or methods for events via +=
⑤ Log out multiple delegate instances or methods for events by -=
⑥ EventHandler is a delegation

I believe that this article has certain reference value for everyone's learning C# programming.