[. NET Basics]-delegate, event, thread (2),. net delegate

Source: Internet
Author: User

[. NET Basics]-delegate, event, thread (2),. net delegate

This article introduces the usage and Principles of the event. This article continues with the Demo of the previous Article [download the previous Demo]

In the previous article, we defined four Delegate proxies in the class (dg_SayHi.cs), and then created and initialized the Delegate object in the background event of the Button, so as to use the Delegate. In this article, we will take a look at the usage and Principles of the event.

1. Now we define a dg_SayHi delegate variable in the Person class, so that we can operate this delegate variable after creating a person class object.

A, Person class add delegate variable

Public class Person {# region name string Name; public string name {get {return name;} set {name = value ;}# endregion # region age int age; public int Age {get {return age;} set {age = value ;}# endregion // defines the delegate variable public dg_SayHi dgSayHi ;}View Code

B. Add the buttons btnPerson and btnPersonDelegate_Click. In the event, we use the delegate object.

Private void btnPersonDelegate_Click (object sender, EventArgs e) {Person per = new Person (); // create a delegate object and assign it to the dgSayHi object per of the person object. dgSayHi = new dg_SayHi (SayHiCN); per. dgSayHi + = SayHiEN; // call the delegate object per. dgSayHi ();}

C. When you click the button, call the SayHiCN and SayHiEN methods to bring up "Hello shanghai" and "hi shanghai" respectively ".

D. Because the delegate object dgSayHi is directly operated, you can directly clear or reset the delegate object added to the method. We update the button background event as follows:

Private void btnPersonDelegate_Click (object sender, EventArgs e) {Person per = new Person (); // create a delegate object and assign it to the dgSayHi object per of the person object. dgSayHi = new dg_SayHi (SayHiCN); per. dgSayHi + = SayHiEN; // the following code clears the methods previously added to the delegate and resets the delegate object per. dgSayHi = null; per. dgSayHi = new dg_SayHi (SayHiEN); // call the delegate object per. dgSayHi ();}View Code

E. As shown above, the delegate object will be unreasonably cleared during the running process, which is insecure.

 

2. to restrict the operations on the delegate object, we define the delegate object as private, and then add methods for operating the delegate object like the attribute

A. Update the Person class, change the delegate to private, and add A method to operate private variables.

Public class Person {# region name string Name; public string name {get {return name;} set {name = value ;}# endregion # region age int age; public int Age {get {return age;} set {age = value ;}# endregion # region protects delegate variables // defines the delegate variable private dg_SayHi dgSayHi; public void AddMethod (dg_SayHi finished) {dgSayHi + = Para_dgSayHi;} public void finished (dg_SayHi finished) {dgSayHi-= finished;} public void PrintMethod () {dgSayHi ();} # endregion}View Code

B. In this way, we cannot directly operate on the delegate object. We only need to add or remove methods to and from the delegate object using two methods similar to attributes. The update Button event is as follows:

     private void btnPersonDelegate_Click(object sender, EventArgs e)        {            Person per = new Person();            per.AddMethod(SayHiCN);            per.AddMethod(SayHiEN);            per.PrintMethod();        }

C. In this way, we cannot directly operate the delegate object, thus realizing the protection of the delegate object.

3. In fact, the. net event helps us to encapsulate it like step 2, so as to implement protection for the delegate object.

A, we add the event declaration delegate again in the Person class as follows:

// Define the event to implement the same public event dg_SayHi dgSayHiByEvent and public void PrintMethodEvent () {dgSayHiByEvent ();}

B. In the background event of the button, the operations on the delegate object can only be added and removed, but cannot be cleared or New, as shown below:

Private void btnPersonDelegate_Click (object sender, EventArgs e) {Person per = new Person (); // create a delegate object and assign it to the dgSayHi object of the person object // per. dgSayHi = new dg_SayHi (SayHiCN); // per. dgSayHi + = SayHiEN; // the following code clears the methods added to the delegate and resets the delegate object. dgSayHi = null; // per. dgSayHi = new dg_SayHi (SayHiEN); per. dgSayHiByEvent + = SayHiCN; // The Event protects the delegate object, which can be + =,-=; but cannot be initialized or cleared as above. dgSayHiByEvent + = SayHiEN; // The delegate object cannot be called directly, but is called through the method of the object. It cannot be written as follows: per. dgSayHiByEvent (); per. printMethodEvent ();}

C ,. NET Reflector, let's look at it. we added the event Delegate, which is actually 1. We created the Private dgSayHiByEvent delegate object 2. The event added two methods: add and remove; these two methods operate on the Private dgSayHiByEvent delegate object. (Similar to set and get)

  

D. Therefore, the essence of the event keyword is to do two things to protect the delegate object:

1. A Private delegate object is created.

2. Then Add and Remove methods to access and operate on this Private delegate object.

  

4. Comparison of delegation and event:

A. The delegate is A class. If you only define the delegate object, you cannot implement protection for the delegate object. You can clear and create new delegate objects externally.

B. Events are delegated objects. The events themselves protect the delegated objects and provide two methods for external access.

C. events can only be added or removed, but cannot be assigned values. Only"Register yourself + =","Cancel yourself -="No =". You cannot log out of another Registrar or trigger an event.

 

[Demo download]

 

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.