Delegate and event

Source: Internet
Author: User

Delegate and event are often used together in C #. What are the mysteries of this process? Here I will talk about the code we see on the following side:

1 using system;
2
3 // delegate
4 delegate void updatedelegate ();
5
6 // subject
7 class subject
8 {
9 public event updatedelegate updatehandler;
10
11 // Methods
12 public void attach (updatedelegate ud)
13 {
14 updatehandler + = UD;
15}
16
17 Public void detach (updatedelegate ud)
18 {
19 updatehandler-= UD;
20}
21
22 public void Policy ()
23 {
24 if (updatehandler! = NULL) updatehandler ();
25}
26
27}
28 public class client
29 {
30 static void updatedelegatemethod ()
31 {
32 console. writeline ("Hello World ");
33}
34
35 public static void main (string [] ARGs)
36 {
37 subject SJ = new subject ();
38 SJ. updatehandler + = new updatedelegate (updatedelegatemethod );
39 SJ. Attach (New updatedelegate (updatedelegatemethod ));
40 SJ. 127y ();
41}
42}

The delegate (updatedelegate) in subjuect has the event keyword before, and its il view is:

If we remove the event keyword before the delegate (updatedelegate), its il view is:

By comparing the two images, we will find that the Il code generated by removing the event keyword by adding the sum of event keys is very different.
Let's take a look at the differences in the main function.
If we remove the event keyword before the delegate (updatedelegate), the main method below is correct and will not report an error during compilation. 1 public static void main (string [] ARGs)
2 {
3 Subject SJ = new subject ();
4 SJ. updatehandler = updatedelegatemethod;
5 SJ. updatehandler = new updatedelegate (updatedelegatemethod );
6 SJ. updatehandler + = new updatedelegate (updatedelegatemethod );
7 SJ. Attach (New updatedelegate (updatedelegatemethod ));
8 SJ. 127y ();
9}

If we add the event keyword 1 public static void main (string [] ARGs) before the delegate (updatedelegate)
2 {
3 Subject SJ = new subject ();
4 // when the event keyword is added, the following two sections of code report an error.
5 // The Event "subject. updatehandler" can only appear on the left side of the "+ =" or "-=" (except for the "subject" type)
6 // SJ. updatehandler = updatedelegatemethod;
7 // SJ. updatehandler = new updatedelegate (updatedelegatemethod );
8 SJ. updatehandler + = new updatedelegate (updatedelegatemethod );
9 SJ. Attach (New updatedelegate (updatedelegatemethod ));
10 SJ. 127y ();
11}

In C #, delegate is of multicast. Multicast means that delegate can point to multiple functions at the same time. A Multicast delegate maintains a list of functions. If we do not use + =, we directly assign the function to delegate. In this way, all other functions that have been hooked with delegate are removed, from this we can see that using event can prevent us from directly assigning functions to delegate, which ensures that the delegate function chain is not damaged.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.