C # events are based on delegates, so the delegate is said first.
The abstract concept of everything out of the real world the novice looks like a rip-duzi, yes, I'm a newbie. So I need a real scene.
Tomorrow is the exam (business English), there are two characters in the exam (class): The Teacher (Teacher) and the student (Student), at the end of the test time, the teacher will trigger (invoke) an event: "The time for the test, I TM to collect the papers!" (Ontesttimeup), and each student subscribes to the event and submits the test paper (Handintestpaper) when the event occurs. If you do not use the event, the teacher will call each student's Handintestpaper method at the end of the time, if the event is used, simply add a subscription to the event when the student is instantiated.
1. Bridge between teacher and student
The bridge between the teacher and the student is done with a delegate, and here is the simplest kind of delegate: no return, no arguments, no generics supported:
Public Delegate void Testtimeuphandler ();
Next, define an event with this delegate so that the student can subscribe to the event
Public Event Testtimeuphandler Testtimeup;
When a student subscribes to an event, the student responds when the teacher triggers the event.
Public void Ontesttimeup () { testtimeup?. Invoke ();}
2. How do students subscribe?
First go back to just the delegate, this delegate is very simple, no return, no parameters, so as long as there is a method in the student class, the signature and this consistent can subscribe to this event:
Public void Handintestpaper () { Console.WriteLine ("Thetest paper have been handed in. " ); }
This method is then subscribed to the event directly after the instantiation is instantiated.
3, the relationship between the two sides
4, The complete example
Delegate
/// <summary> /// entrusted, the time of the examination to the handler /// </summary> Public Delegate void Testtimeuphander ();
Teacher
/// <summary> /// Teacher Class /// </summary> Public class Teacher { publicevent testtimeuphander testtimeup; Public void ontesttimeup () { testtimeup? . Invoke (); } }
Student
/// <summary> ///Student Class/// </summary> Public classStudent { Public stringName {Get;Private Set; } PublicStudent (stringname) { This. Name =name; } Public voidHandintestpaper () {Console.WriteLine ($"{this. Name}\ ' paper have been handed in"); } }
5. Concrete Scene Realization
Public classProgram { Public Static voidMain () {Teacher Teacher=NewTeacher (); varTom =NewStudent ("Tom"); varJerry =NewStudent ("Jerry"); varSpark =NewStudent ("Spark"); varTyke =NewStudent ("Tyke"); //Subscribe to the teacher Testtimeup eventTeacher. Testtimeup + =Tom. Handintestpaper; Teacher. Testtimeup+=Jerry. Handintestpaper; //Invoke Testtimeup Eventteacher. Ontesttimeup (); Console.WriteLine (); Teacher. Testtimeup-=Tom. Handintestpaper; Teacher. Testtimeup+=Tyke. Handintestpaper;
Teacher. Ontesttimeup (); Console.readkey (); } }
The results of the operation are as follows:
As you can see, the first time the event was triggered, only Tom and Jerry two instances of the subscribed event executed the Handintestpaper method, and the second time the event was triggered, Tom's subscription was removed, and Tyke's subscription was added. So Jerry and Tyke executed the Handintestpaper method.
Above, is the simplest C # delegate and event implementation, complex method later
C # Delegates and Events (i): Simplest delegates and events