The difference between delegates and events in C# Basically, a delegate is a class that internally maintains a field pointing to a method. An event can be thought of as a variable of delegate type, through which multiple delegates or methods are registered and cancelled. In this article, you’ll see the difference between executing several methods through delegates and events.

□ By delegated execution method

class Program { static void Main(string[] args) { Example example = new Example(); example.Go(); Console.ReadKey(); } } public class Example { public delegate void DoSth(string str); Public void Go() {public void Go() {public void Go() {public void Go() {public void Go(); string str = "Hello,World"; // Evoke the delegate D.Invoke (STR) via the delegate static method Invoke; } void Print(string str) { Console.WriteLine(str); }}

The above,

At CLR runtime, delegate DoSth is actually a class that takes a constructor of type method and provides an Invoke instance method to Invoke the delegate. ○ By delegating the constructor to doSth, we can assign a method that matches the definition to delegate ○. The instance method Invoke Invoke executes the method

However, there is actually another way to get a delegate to execute a method, and that is to delegate a variable (parameter list).

public class Example { public delegate void DoSth(object sender, EventArgs e); Public void Go() {public void Go() {public void Go() {public void Go() {public void Go(); object sender = 10; EventArgs e = new EventArgs(); d(sender, e); } void Print(object sender, EventArgs e) { Console.WriteLine(sender); }}

The above,

Object sender is used to represent the initiator of the action, and EventArgs e is used to represent the parameters of the action.

In fact, delegate variables (parameter lists), and events perform methods this way.

□ Execute methods through events

public class Example { public delegate void DoSth(object sender, EventArgs e); public event DoSth myDoSth; Public void Go() {public void Go() {public void Go() {public void Go() {public void Go(); 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,

A delegate is registered for the event by using +=. A delegate is registered for the event by using the constructor of the DOSth delegate. A delegate is registered for the event by using the delegate variable (parameter list) to make the event execute

Also, you can register multiple delegates for events with +=.

public class Example

{ public delegate void DoSth(object sender, EventArgs e); public event DoSth myDoSth; Public void Go() {public void Go() {public void Go() {public void Go() {public void Go(); DoSth d1 = new DoSth(Say); object sender = 10; EventArgs e = new EventArgs(); MydoSth += new doSth (d); 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, register one or more delegate instances for events with +=, and in fact, register methods for events directly.

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(); 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); }}

□ Execute the method through EventHandler

Let’s start with the source code for EventHandler.

1

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

public class Example { public event EventHandler myEvent; internal void Go() { object sender = 10; EventArgs e = new EventArgs(); MyEvent += Print; 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); }}

Summary: A delegate is a class that can also be instantiated. There are two ways to trigger a delegate: a delegate instance. ○ An event can be regarded as a variable of delegate type ○ Registered multiple delegate instances or methods for the event by +=. ○ Unregistered multiple delegate instances or methods for the event by -=