2018-05-01 10:49:47
A delegate is a type, and an event is a member of a class or struct, as in a field, property. Must be declared in a class or struct.
Extended-Observe Viewer mode Publish/subscribe Publish-Subscribe mode
A basic form
1. Declare the delegate, build the event in the class, and trigger the code as shown
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceEventdemo8 {9 //Event 1 of 5 elements--Deletegate declareTen Delegate voidHandler (); One A //Publisher - //1. Event Declare - //2. Fire event code the classIncrementer - { - Public EventHandler eventpub;//Create Event - + //code that triggers the event - Public voiddoincrement () + { A for(inti =0; I < -; i++) at { - ifI A==0&& eventpub! =NULL) - eventpub (); - } - } - in } -}View Code
2. Create event handlers and register events in subscribers.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceEventdemo8 {9 /*Ten * Subscribers One * 1. Event handlers A * 2. Inject Publisher into the constructor and construct the code to add a handler method to the event. Subscribe to events. - */ - classSubscribecode the { - Public intDozonecount {Get;Private Set; } - - //Event Handlers + voidIncrementcount () - { +dozonecount++; A } at - //Constructor Subscription Event - PublicSubscribecode (Incrementer Inc) - { -Dozonecount =0; -Inc.eventpub + =Incrementcount; in } - to } +}View Code
Two EventHandler
Public vodi EventHandler (Object sender, EventArgs e)
(this, null) to match a consistent
Three custom EventArgs derived classes are required to transfer data.
1. First define the class of the derived child EventArgs
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceEventdemo.customerdefine8 {9 /*Public void EventHandler (Object sender, EventArgs e)Ten * EventArgs is designed to not pass any parameters, One * You need to declare a class derived from EventArgs and customize the fields that hold the data. A * - * Naming specification ends with args - */ the Public classCustomereventargs:eventargs - { - Public intDestinatedata {Get;Set; } - } +}custom EventArgs Derived classes
2. The remaining steps are as follows, note that when you define an event handler
1) The return value is void.
2) Publisher
View Code
3) Subscribe
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceEventdemo.customerdefine8 {9 classSubscribeclassTen { One Public intTransferData {Get;Set; } A - Public voidAccountmethod (Object send, Customereventargs e) - { theConsole.WriteLine ("{0} transfer data,/t {1} send", - E.destinatedata, send. ToString ()); -transferdata++; - } + Publicsubscribeclass (publisherclass pc) - { +TransferData =0; APc.handler + =Accountmethod; at } - } -}View Code
Note The Remove event handler is removed from the last look for the first match, similar to the delegate's method
Advanced usage, Event accessors
Change + = = To. Let the event execute any custom code that we want.
Two accessors Add remove
Declaring an event accessor is similar to declaring a property that returns void
Public event EventHandler Countedadozen
{
Add
{ ... } Execute + = operator
Remove
{ ... } Execute-= operator
}
After the declaration, the event does not contain any inline delegate objects. You must implement your own mechanism for storing and removing time registration methods.
Software--c#--grammer_delegate--event