C # delegation event entry-Lecture 2: event entry,

Source: Internet
Author: User

C # delegation event entry-Lecture 2: event entry,

C # delegation event entry-lecture 1: Delegation events are introduced in this article. If you want to learn delegation, you must talk about the events. Here we will explain what the event is, why it is used, how it is implemented, and how it is summarized.

1. What is an event :.

1.1. NET events are built on the delegate mechanism, and events are the encapsulation of the delegate.

1.2 classification of events: Strong events and weak events

2. Why event:

From the introduction to delegation, we can understand that we can operate the delegation at Will on the client, which destroys the object-oriented encapsulation mechanism to a certain extent, therefore, the event is generated to encapsulate the delegate.

3. How to Implement events:

In fact, the definition of the event is very simple, but it depends on the delegate: the keyword event is the identifier of the event.

/// <Summary> /// 1. Define a delegate /// </summary> /// <param name = "x"> </param> public delegate void BoilHandler (int x ); /// <summary> /// 2. Declare an event // </summary> public event BoilHandler boilHandler;

That is, in the code that describes the delegation entry above. Declare a delegate method and add the keyword event,

The complete code is as follows:

Using System; namespace DelegateDemo {public class Program {public void Write1 (int x, int y) {Console. writeLine ("{0} + {1} = {2}", x, y, x + y);} public static void Write2 (int x, int y) {Console. writeLine ("{0} * {1} = {2}", x, y, x * y);} static void Main (string [] args) {GManage gm = new GManage (); gm. delegate1 + = new Program (). write1; // assign gm to the delegate type variable first. delegate1 + = Program. write2; // bind the delegate variable to a static method gm. SayHello (1, 2); // call the Write1 and Write2 Methods Console successively. writeLine ("******* after the unbinding method Write2 ******"); gm. delegate1-= Program. write2; // unbind gm from the delegate method. sayHello (1, 2); // After unbinding, only the Write1 Console is called. readKey () ;}} public class GManage {/// <summary> /// 1. Define the delegate, it defines the types of methods that can be represented /// </summary> /// <param name = "x"> </param> /// <param name = "y "> </param> public delegate void GreetingDelegate (int x, int y); // <summary> /// 2. Declare an event // </summary> public event GreetingDelegate delegate1; /// <summary> /// 3. Method of the called deleGATE /// </summary> /// <param name = "x"> </param> // /<param name = "y"> </param> public void SayHello (int x, int y) {if (delegate1! = Null) // if there is a method to register the delegate variable {delegate1 (x, y); // call the method through delegation }}}}

However, the registration is different from the registration of the Delegate, because the event only provides two external Methods: + = and-= to bind and unbind methods, if the value = is still used, the program will report an error.

4. Summary

An event is essentially a delegate. Two access methods add_EventName (corresponding to + =) and remove-EventName (corresponding to-=) are provided externally to bind and unbind methods, at the same time, it is more in line with the object-oriented encapsulation and security.

The code word is not easy. Please indicate the source for reprinting. Thank you.

Source code link: https://github.com/YaoHigh/DelegateDemo

 

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.