Distinction instance resolution of delegates and events in C

  • 2020-09-16 07:45:35
  • OfStack

This article analyzes the differences between delegates and events in C# and shares them with you for your reference. The details are as follows:

Roughly speaking, a delegate is a class that internally maintains a field pointing to a method. An event can be thought of as a variable of a delegate type, registering and canceling multiple delegates or methods by event. This article examines the difference between delegating and executing multiple methods through events.

1. Execution by delegation


class Program
{
    static void Main(string[] args)
    {
      Example example = new Example();
      example.Go();
      Console.ReadKey();
    }
}
public class Example
{
    public delegate void DoSth(string str);
    internal void Go()
    {
      // The statement 1 Three delegate variables and take a known method as an argument to its constructor 
      DoSth d = new DoSth(Print);
      string str = "Hello,World";
      // Static methods through the delegate Invoke Trigger commissioned 
      d.Invoke(str);
    }
    void Print(string str)
    {
      Console.WriteLine(str);
    }
}

The above code is implemented:

When CLR is running, the delegate DoSth is actually a class that has a constructor with a parameter type of a method and provides an instance method of Invoke to trigger the execution of the delegate.
Delegate DoSth defines the parameters and return types of the methods
By delegating DoSth's constructor, you can assign a defined method to the delegate
Call the delegate's instance method Invoke executes the method

But there's actually another way to get the delegate method, and that's the delegate variable.


public class Example
{
    public delegate void DoSth(object sender, EventArgs e);
    internal void Go()
    {
      // The statement 1 Three delegate variables and take a known method as an argument to its constructor 
      DoSth d = new DoSth(Print);
      object sender = 10;
      EventArgs e = new EventArgs();
      d(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      Console.WriteLine(sender);
    }
}

The above code is implemented:

The parameter list of delegate DoSth and the parameter list of method Print remain 1
The parameter object sender in the delegate DoSth is usually used to represent the initiator of the action, while EventArgs e is used to represent the parameter of the action.

In fact, the delegate variable (argument list), the event is executed in this form.

2. Through the event execution method


public class Example
{
    public delegate void DoSth(object sender, EventArgs e);
    public event DoSth myDoSth;
    internal void Go()
    {
      // The statement 1 Three delegate variables and take a known method as an argument 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)
    {
      Console.WriteLine(sender);
    }
}

The above code is implemented:

Declares the event myDoSth, the event type is DoSth this delegate
Register the delegate for the event by +=
Register the delegate instance for the event through the constructor of the DoSth delegate
(4) using the delegate variable (parameter list) this form, let the event execution method

Also, multiple delegates can be registered for events with +=.


public class Example
{
    public delegate void DoSth(object sender, EventArgs e);
    public event DoSth myDoSth;
    internal void Go()
    {
      // The statement 1 Three delegate variables and take a known method as an argument to its constructor 
      DoSth d = new DoSth(Print);
      DoSth d1 = new DoSth(Say);
      object sender = 10;
      EventArgs e = new EventArgs();
      // Register multiple delegates for the event 
      myDoSth += new DoSth(d);
      myDoSth += new DoSth(d1);
      myDoSth(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      Console.WriteLine(sender);
    }
    void Say(object sender, EventArgs e)
    {
      Console.WriteLine(sender);
    }
}

Above, you can register one or more delegate 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 delegates for the event 
      myDoSth += Print;
      myDoSth += Say;
      myDoSth(sender, e);
    }
    void Print(object sender, EventArgs e)
    {
      Console.WriteLine(sender);
    }
    void Say(object sender, EventArgs e)
    {
      Console.WriteLine(sender);
    }
}  
 

3. Implementation method via EventHandler

Let's start with the EventHandler source code.


public delegate void EventHandler(object sender, System.EventArgs e);

So EventHandler is the delegate. Now use EventHandler to perform multiple methods.


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

Conclusion:

A delegate is a class that can also be instantiated by assigning a method to a delegate instance via the delegate's constructor
(2) There are two ways to trigger the delegate: delegate instance.Invoke(parameter list), delegate instance (parameter list)
The event can be thought of as a variable of type 1 delegate
Register multiple delegate instances or multiple methods for the event through +=
(5) Unregister multiple delegate instances or methods for the event by -=
EventHandler is one delegate

I believe that what I have described in this paper is of certain reference value to the learning of C# programming.


Related articles: