This article mainly introduces the difference between the delegate and event in C #, and shows the method of executing by the delegate and the execution of the event by instance, as well as the related execution process and principle analysis, and the friend who needs can refer to the example of the case analysis of the difference between the delegate and event in C #, and share it for everyone's reference. Specific as follows:
In general, a delegate is a class that maintains a field inside the class that points to a method. An event can be treated as a variable of a delegate type, registered through an event, and canceled by multiple delegates or methods. This article, through the delegates and events to execute several methods, to understand the difference between the two.
I. By means of delegated execution
12345678910111213141516171819202122232425 |
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()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
DoSth d =
new DoSth(Print);
string str =
"Hello,World"
;
//通过委托的静态方法Invoke触发委托
d.Invoke(str);
}
void Print(
string str)
{
Console.WriteLine(str);
}
}
|
The code above implements:
① when the CLR runs, the delegate dosth actually has a class that has a constructor for the method and an invoke instance method that triggers the execution of the delegate.
② delegate dosth defines the parameter and return type of the method
③ by delegating the constructor of the dosth, you can assign a method that conforms to the defined value to the delegate
④ invokes the instance method of the delegate invoke executes the method
But there is another way to actually let the delegate execute the method: the delegate variable (parameter list)
12345678910111213141516 |
public class Example
{
public delegate void DoSth(
object sender, EventArgs e);
internal void Go()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
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 code above implements:
① the parameter list of the delegate dosth and the parameter list of the method print remains consistent
② the argument in the delegate Dosth object sender is typically used to represent the initiator of the action, and EventArgs E is used to denote the parameters that the action takes.
In fact, the delegate variable (the argument list), the event is the execution method in this form.
Second, through the implementation of the event method
123456789101112131415161718 |
public class Example
{
public delegate void DoSth(
object sender, EventArgs e);
public event DoSth myDoSth;
internal void Go()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
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 code above implements:
① declares the event mydosth, the type of the event is dosth this delegate
② registering a delegate for an event via + =
③ registering a delegate instance for an event through the constructor of the Dosth delegate
④ takes the form of a delegate variable (a parameter list) to let the event execution method
Also, you can register multiple delegates for an event by using + =.
12345678910111213141516171819202122232425 |
public class Example
{
public delegate void DoSth(
object sender, EventArgs e);
public event DoSth myDoSth;
internal void Go()
{
//声明一个委托变量,并把已知方法作为其构造函数的参数
DoSth d =
new DoSth(Print);
DoSth d1 =
new DoSth(Say);
object sender = 10;
EventArgs e =
new EventArgs();
//为事件注册多个委托
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, by registering 1 or more delegate instances with + = For the event, you can actually register the method directly for the event.
12345678910111213141516171819202122 |
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);
}
}
|
Iii. methods of implementation through EventHandler
First look at the source code of EventHandler.
1 |
public delegate void EventHandler( object sender, System.EventArgs e); |
Visible, EventHandler is the delegate. Use EventHandler to execute multiple methods now.
123456789101112131415161718192021 |
public class Example
{
public event EventHandler myEvent;
internal void Go()
{
object sender = 10;
EventArgs e =
new EventArgs();
//为事件注册多个委托
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);
}
}
|
Summarize:
A ① delegate is a class that can also be instantiated,
The difference instance resolution of delegates and events in C #