When I first saw the Commission, I directly thought of the interface, to recall the interface.
When we are going to do something, we need something, we ask this thing to do something, but it wants to do exactly what we do not know or care about, so we define an interface, which is all abstract methods, to tell others we ask to do these things. Then throw the interface to someone else to implement it. Let's just call the interface.
I understand the Commission is the same, like you have to build a house for yourself, you draw the design drawings, and then throw to the secretary, said to him, "you go to find some people, let them give me the house built." ”
Is it similar to the interface?
But if you just want someone else to help you do one thing, and that person can do a lot of his own things, only when you ask him to do it when you need him to do, define an interface is not very necessary?
A delegate, which is a reference type, that points to a method. So when defining a delegate, it looks more like defining a method than defining a class.
Define a delegate public delegate int PlayMedia ();//Declare a delegate instance public event PlayMedia PlayMedia;
Use the delegate keyword to create a delegate.
The event keyword tells the editor that the delegate can only define the class invocation of the delegate, and that it can only be used by other classes to subscribe to or unsubscribe from the + = and-= operators, and of course there is no event keyword.
The implementation method of a delegate has the same return type and signature as the delegate, but both non-static and static methods are possible.
Event:
An event is actually a delegate, but a delegate can be anything, but an event usually refers to some action or some state change, and the events in. NET have their own specifications.
The event handlers in. NET typically return a void type, and get two arguments, the first parameter is an instance of the class that defines the delegate, and the second argument is a derived class object that EventArgs (the base class for the event data).
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace Consoleapplication1{class Program {static void Main (string[] args) {Clock Clock = new Cl Ock (); (implement delegate) bind event Clocka a = new Clocka (); A.subscribe (clock); CLOCKB B = new clockb (); B.subscribe (clock); Call the clock's Run method clock. Run (); }}///<summary>//Event parameter class///</summary> class Timeinfoeventargs:eventargs {Publ IC int Hour {get; set;} public int Minute {get; set;} public int Second {get; set;} Public Timeinfoeventargs (int hour, int minute, int. second) {this. Hour = Hour; This. Minute = Minute; This. Second = Second; }}///<summary>///Clock class///</summary> class Clocks {//Declare a private variable to store the current second PRI vate int Second; <summary>///define a delegate (seconds Change trigger Event)////</summary>//<param name= "Clock" ></para m>//<param name= "E" ></param> public delegate void Secondchangehandler (object clock, Timein Foeventargs e); Declares a delegate instance public event Secondchangehandler secondchanged; public void Run () {for (;;) {//Pause 100 ms Thread.Sleep (100); Gets the current system time System.DateTime dt = System.DateTime.Now; If seconds change if (DT). Second! = Second) {//If there is a Subscriber (event trigger) if (secondchanged! = null) {//Notify Subscribers (trigger event) secondchanged (this, new Timeinfoeventargs (dt. Hour, dt. Minute, dt. Second)); }//Change the stored seconds this.second = dt. Second; } } }} class Clocka {//The delegate to the clock class is bound to public void Subscribe (clock clock) {clock. Secondchanged + = new Clock.secondchangehandler (timehaschanged); } public void Timehaschanged (object clock, Timeinfoeventargs e) {System.Console.WriteLine ("subsc Ribea Current time: {0}:{1}:{2} ", E.hour.tostring (), e.minute.tostring (), e.second.tostring ()); }} class Clockb {public void Subscribe (clock clock) {clock. Secondchanged + = new Clock.secondchangehandler (timehaschanged); } public void Timehaschanged (object clock, Timeinfoeventargs e) {System.Console.WriteLine ("subsc Ribeb Current time: {0}:{1}:{2} ", E.hour.tostring (), e.minute.tostring (), e.second.tostring ()); } }}
C # delegates and events