Clarify delegation and events

Source: Internet
Author: User

I,Delegated what

A container that can store pointers for methods (method signatures) in a certain format

II,Syntax

Demo1

DGSayHi (Page_Load (DGSayHi dgSayHi = dgSayHi (Response. Write (SayHiSomeWhere (Response. Write (location +}View Code

Custom button: Demo2

MyButton class:

. Click + = MyButton_Click (dgMyClick! = DgMyClick (DateTime. Now );}View Code

Demo2 call:

Page_Load (MyButton m = m. Text = m. dgMyClick = Response. Write (. Format (}View Code

III,Syntactic sugar

In c #, there are many concise syntaxes. The essence is to convert the compiler into the complete syntax during compilation.

Demo3

Page_Load (Button1_Click (DGTest dg = Test1; dg + = Test2; dg + = Test3; dg-= Test3; dg (); Response. write (Response. write (Response. write (}View Code

IV,Delegate

1. After delegation compilation, a class with the same name is claimed, such as DGTest, which inherits from MulticastDelegate and MulticastDelegate inherits from Delegate.

2. The essence of the Delegate append method is to create a new Delegate object for the appended method and store the method pointer to the parent class (Delegate) of the object's parent class) intPrt (a data type dedicated to storing pointers) variable, and then add the newly created Delegate to the array of the current delegate object (dg.

V,Purpose of delegation

1. Delegate when return value demo4

Page_Load (Unnamed1_Click (DGTest dg = InvokeTest (DGTest InvokeTest (Response. Write (}View Code

2. method when the demo5 Parameter

Page_Load (button#click (Response. Write (}View Code

VI,Delegated processing string

Demo6

DGMakeUpStr (Page_Load (MakeUpStr (str + = [] MakeUpStrArr ([] arrstrNew = (I =; I <arrStr. length; I ++ arrstrNew [I] = button#click ([] strArr = [] {, [] strArrNew = (I =; I <strArrNew. length; I ++}View Code

VII,Disadvantages of delegation

Click = null can be used to clear all registered events.

Click can be used to trigger a "fake" event.

Solution: Set the delegate member to private and add AddClickEventHandler and RemoveClickEventHander public methods.

Demo7

Page_Load (MyButton m = m. Text = m. dgMyClick + = Response. Write (. Format (}View Code

Optimization Method:

MyButtonSelf class:

DgMyClick + = dgMyClick-=. Click + = MyButtonSelf_Click (dgMyClick! = DgMyClick (DateTime. Now );}View Code

8,Event

The event mechanism automatically changes the modified delegate variable to private, and provides both the add and remove methods.

The delegated optimized version.

Demo8

MyButtonEvent class:

. Click + = MyButtonSelf_Click (dgMyClick! = DgMyClick (DateTime. Now );}View Code

Call class:

Page_Load (MyButtonEvent m = m. Text = m. dgMyClick + = MyButtonClick; Response. Write (. Format (}View Code

 

 

Certificate ------------------------------------------------------------------------------------------------------------------------------------------

Soft seek online education is the most suitable for online software training for college students and office workers. It mainly teaches asp.net dynamic website production and remote real-time teaching in the yy education room. Each course is recorded as a high-definition video and shared after class, the instructor provides 8 hours of full-time online tutoring during the day. Add QQ group: 138800420 for free trial.

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.