C # differences between delegate and event,

Source: Internet
Author: User

C # differences between delegate and event,

The following describes the events.

1. Define event and event compilation principles

First, define the delegate corresponding to the event:

1 public delegate void ThreeClickEventDel();

 

Then it corresponds to the event variable: (in fact, an event is added before the delegate)

public event ThreeClickEventDel ThreeClickEvent;

 

After decompilation: a "private delegate" and two "public" "add" and "remover" Methods

1) private delegation: only the class can be called internally and cannot be called outside the class. The instance of the created class cannot be called. Therefore, events are safe.

Because it is private, you cannot use "=" to assign values to events.

2) because there are "public" add and remove ,, therefore, the class instance can perform "+ =" and "-=" on the event to add events and delete events (so the event can be assigned a value outside the class, but cannot be called)

 

2. Differences between delegation and events:

1. Data Type During delegation, but events are data-type variables (and private delegation)

2. The delegate can be called outside the class, but the event cannot be called outside the class through the class instance (because it is a private delegate)

3. Delegates can assign values through "=", "+ =", and "-=", but events can only assign values to events through "+ =" and "-=. (Because the event is a private delegate)

 

3. The delegate can also register events. Why are events generated?

Delegated registration event:

1 // define a delegate 2 public delegate void JingGaoDel (); 3 class Program 4 {5 static void Main (string [] args) 6 {7 GuoluFang Guolu = new GuoluFang (); 8 // for delegated registration event 9 Guolu. jingGao = delegate () 10 {11 Console. writeLine ("the water has been burned out! "); 12}; 13 for (int I = 0; I <100; I ++) 14 {15 Guolu. shaoShui (); 16} 17 Console. readKey (); 18} 19}

 

Why derivative events:

1. Because an event can only be assigned values through "+ =" and "-=", you can avoid using the "=" value assignment as the delegate (the original event may be rushed out ).

2. Although an event is defined as public, it is compiled into a "private delegate". Therefore, an event can only be called within the class, and the instance of the class cannot be called.

 

4. Use event exercises (view Microsoft events)

Create a verification control:

Add a UserControl control. The design is as follows:

Write background code:

Control background code

 

Add verification controls to the main form:

Background code:

1 private void Form1_Load (object sender, EventArgs e) 2 {3 // register event 4 userVilidateControl1.UserViliDating + = new UserVilidateDel (userVilidateControl1_UserViliDating ); 5} 6 7 // create the corresponding method event 8 void uservilidatecontrol?uservilidating (object sender, MyEventArgs e) 9 {10 if (e. userName = "admin" & e. pwd = "123") 11 {12 e. isSuccess = true; 13} 14}

 

Related Article

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.