C # differences between delegate and event

Source: Internet
Author: User

1, what is commissioned, here will not be introduced, If You Want To Know can view blog: http://www.cnblogs.com/xiaoxiaogogo/p/3571494.html

The following describes the events.

First, define the delegate corresponding to the event:

    ThreeClickEventDel();

 

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

  ThreeClickEventDel ThreeClickEvent;

 

After decompilation: A "" and two "and" "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 "" 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)

 

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)

 

Delegated registration event:

                             Main(              GuoluFang Guolu =                           Guolu.jingGao =                   Console.WriteLine(               ( i = ; i <; i++          }

 

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.

 

Create a verification control:

Add a UserControl control. The design is as follows:

Write background code:

UserVilidateDel (btnLogin_Click (userName = pwd = MyEventArgs eArgs = eArgs. userName = eArgs. pwd = eArgs. isSuccess = UserViliDating (. txtUserName. backColor =. txtPwd. backColor =. txtUserName. backColor =. txtPwd. backColor = UserName {; Pwd {; IsSuccess {;}Control background code

 

Add verification controls to the main form:

Background code:

           Form1_Load(                           userVilidateControl1.UserViliDating +=                      userVilidateControl1_UserViliDating(               (e.UserName ==  && e.Pwd ==                   e.IsSuccess =           }

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.