C # graphic tutorials study notes - Event

First, the definition of an event
an event: when a particular program event, other parts of the program may be notified that the event has occurred while running the appropriate handler.
Many are part of the event is similar to the delegate. In fact, a special event like a simple delegate for special purposes.
Events include a private commission, the event is triggered, it calls the delegate list to turn call the method call.

 

Second, use the steps of the event
delegate type 1. Declare the event of
events and event handlers must have a common signature and return type, which is described by the delegate type event.
BCL (Base Class Library, base class library) declares a delegate named EventHandler, and devoted to system events.
2. Declare the event
(1) declare events within the event publisher class.
(2) when the event declared as public, he called the publisher of the event.
(3) members are implicitly event automatically initialized to null.
Example:

Note: You can not use the object creation expression (new expression) to create the publisher objects.
3. Declare event handler
event subscribers need to declare a method performed when the event is triggered, that event handler.
Event handler method signature and return type must return the same type of event and delegate type signature.
4. Event Subscribe
event subscriber using the + = operator to increase the event handler for the event, the event is called subscription.
The event handler can be instance methods, static methods, anonymous methods and Lambda expressions.
The same may be used - = operator to remove the event handler from the event. If a handler is registered to the event several times, then remove the list of programs, the last instance of the handler will only be removed.
The trigger event
trigger event syntax and call a method of the same:
using the event name, followed by the parameter list.
The list of parameters required to match the event delegate type.
Event source components illustrated:

Events using the full sample:

Guess you like

Origin www.cnblogs.com/wujuntian/p/10990342.html