C # learning events

Source: Internet
Author: User

 

 

. The event model of net is built on the delegate mechanism. Event mechanisms can be implemented, and Native delegation can also be implemented. It can be said that events are only the encapsulation of the Delegate, making it safer to use.

In a specific code, the difference between defining a delegate and defining an event is that you must add an event keyword before defining an event!

Because of this keyword, the compiler will secretly do a lot of things for the encapsulated delegate. For example, native delegate can be initialized directly with new, when the delegate chain is bound to many methods, a = operator can be directly used to clear those methods, and so on. For events, the compiler encapsulates these methods and cannot confuse them, the compiler only provides two methods: add_myevent and remove_myevent, allowing the event client to add and remove event subscribers only through + = and-=. If there is no subscriber (that is, the delegate chain is empty) when the event is triggered, nothing will happen, etc.

 

Everything is in the code.

 

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. io; 6 using system. threading; 7 8 9 namespace test10 {11 12 // defines the message class 13 class calculateeventagrs: eventargs14 {15 public readonly int X, Y; 16 public calculateeventagrs (int x, int y) 17 {18 this. X = x; 19 this. y = y; 20} 21} 22 23 24 // define event release Class 25 class calculator26 {27 // define a proxy class, declare event Delegate 28 public DELE Gate void calculateeventhandler (Object sender, calculateeventagrs E); 29 30 // defines the event member and proxy class type, and provides 31 public event calculateeventhandler mycalculate for external binding; 32 33 // provides protected virtual methods, which can be overwritten by sub-classes to reject monitoring 34 protected virtual void oncalculate (calculateeventagrs e) 35 {36 // if no subscriber exists, do nothing 37 If (mycalculate! = NULL) 38 {39 // trigger event 40 mycalculate (this, e); 41} 42} 43 44 45 // trigger event method, that is, the event occurs, it can be understood as a mouse click event 46 Public void calculate (int x, int y) 47 {48 // message generated when an event occurs 49 calculateeventagrs E = new calculateeventagrs (X, y); 50 51 // notify registrants of all events 52 oncalculate (E ); 53} 54 55} 56 57 58 // define the event receiving Class/event Subscriber/59 class calculatormanager60 {61 // event processing function, which can change the class (that is, the event subscriber) 62 public void add (Object sender, calculateeventagrs e) 63 {64 console. writeline (E. X + E. y); 65} 66 67 // event handler function 68 public void subtract (Object sender, calculateeventagrs e) 69 {70 console. writeline (E. x-e. y); 71} 72} 73 74 75 class program76 {77 static void main (string [] ARGs) 78 {79 calculator calculate = new calculator (); // event publisher 80 81 calculatormanager CM = new calculatormanager (); // event Subscriber/receiver 82 83 // calculate. mycalculate = new calculator. calculateeventhandler (); 84 85 86 calculate. mycalculate + = cm. add; // subscribe to the event: Cm actively subscribes to the mycalculate event 87 88 calculate in the calculate object. mycalculate-= cm. subtract; // unsubscribe: If this event does not exist in the delegate chain, no exception is thrown and no calculation is performed. calculate (100,200); // event 91 92 console. readkey (); 93} 94 95} 96}

 

In line 83, if the comment is removed and then compiled, the following error will be prompted:

If you use the following code: Calculate. mycalculate = cm. Add;, the same error will be prompted.

 

 

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.