Ah_bill has a lot of knowledge about Java, while C # has a look at it only because of work needs. C # is very similar to Java in syntax, at first, I was confused about delegation and events. I believe that most beginners have similar confusions. After comparison and learning with Java, we found that this is actually the same as Java listening and events, but it is different in expression.
The delegate + event is a typical example of the observer mode. The so-called delegate is actually the observer, which cares about an event. Once this event is triggered, the observer will act.
The following is an example recently written. I believe it can help you better understand the delegation and events.
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace consoleapplication3
{
Public Delegate void timeeventhandler (Object OBJ, timeeventargs ARGs); // defines a delegate. A delegate is actually a "method template", just like a "class" is an "object" template. If a class wants to receive a notification when an event is triggered, it must have a method in this format. In this example, the return type is void, the parameter types are object and timeeventargs.
// Timeeventargs is a defined class used to save parameters in the event. Here we save the time, minute, and second respectively.
Public class timeeventargs: eventargs {
Private int hour;
Private int minute;
Private int second;
Public timeeventargs (INT hour, int minute, int second)
{
This. Hour = hour;
This. Minute = minute;
This. Second = second;
}
Public int hour
{
Get
{
Return hour;
}
}
Public int minute
{
Get
{
Return minute;
}
}
Public int second
{
Get
{
Return second;
}
}
}
// This is an observer class. It has a method that complies with the "delegate" defined above, that is, void Showtime (Object OBJ, timeeventargs ARGs ), from the definition of this method, we can see that we only care about the return type and method parameters, but the method name does not matter.
Class mytimeeventhandlerclass
{
Public void Showtime (Object OBJ, timeeventargs ARGs)
{
Console. writeline ("the current time is:" + args. Hour + ":" + args. Minute + ":" + args. Second );
}
}
// Clock type
Class clock
{
// We define a "timechanged" event in this class. Note that there are two keywords before it: "Event" and "timeeventhandler", where event indicates that this is an event, instead of a method or attribute. timeeventhandler indicates that to listen to a timechanged event, it must have a method that complies with timeeventhandler (delegate.
Public event timeeventhandler timechanged;
Public clock ()
{
Timechanged = NULL; // Note: NULL indicates that the timechanged event has not been followed by the observer. If an Observer wants to pay attention to the timechanged event, it must let the event know, the method is to use the operator "+ =" to load it to the event with the delegate.
}
// The clock starts to move around. Our goal is to trigger a timechanged event every second.
Public void go ()
{
Datetime initi = datetime. now;
Int H1 = initi. hour;
Int M1 = initi. minute;
Int S1 = initi. Second;
While (true)
{
Datetime now = datetime. now;
Int H2 = now. hour;
Int m2 = now. minute;
Int S2 = now. Second;
If (s2! = S1)
{
H1 = h2;
M1 = m2;
S1 = S2;
// Create a timeeventargs object to save relevant parameters. The time is minute/second.
Timeeventargs ARGs = new timeeventargs (H2, M2, S2 );
// Pay attention to this writing method. This sentence is used to trigger events. events are not classes, so you do not need to use the "new" keyword. We can see that, here, the two timechanged parameters are consistent with our delegate (timeeventhandler). The first parameter is the object that triggers this event. Here we use a clock instance (this ).
Timechanged (this, argS );
}
}
}
}
Class Program
{
Static void main (string [] ARGs)
{
Clock = new clock (); // instantiate a clock
Mytimeeventhandlerclass tehc = new mytimeeventhandlerclass (); // instantiate an observer class
// Connect the event to the Observer we define. In this way, clock will know that every time the timechanged event is triggered, it will notify the observer, note that we use not the Showtime () method in the direct observer class instance during connection, but a delegate and pass the Showtime () method in this delegate, this is also the true meaning of "delegation"-I have a method, but I entrust you to help me relate to the event, because the event will only deal with the delegation directly, instead of the observer's specific method.
Clock. timechanged + = new timeeventhandler (tehc. Showtime );
Clock. Go ();
}
}
}