Let's take a look at the delegation, delegate is actually the transmission of the method, and does not define the implementation of the method. The incident is actually a standardized delegation, in order to process the process processing, a little professional, a little professional, a little delegation (multi-delegated). Let's take an example, I think it is easier to understand the examples of delegated examples and events.
code:
Using system;
Class class1
{
Delegate Int Mathop (INT I1, INT I2);
Static void main (string [] args)
{
Mathop op1 = new mathop (add);
Mathop op2 = new mathop (multiply);
Console.WriteLine (OP1 (100, 200));
Console.WriteLine (OP2 (100, 200));
Console.readline ();
}
Public Static Int Add (Int I1, INT I2)
{
RETURN I1 I2;
}
Public Static Int Multiply (int I1, INT I2)
{
Return I1 * I2;
}
}
First, the code defines a delegate Mathop, its signature match with two functions add (), and multiply (), the same number of parameter types): DELEGATE INT MATHOP (Int I1, I2); main () The middle code first declares a variable using the new entrustment type, and initializes the delegate variable. Note that the parameters when the declaration, as long as the function name of the function passed, not parentheses: mathop op1 = new Mathop (or for Mathop op1 = new mathop (multiply);) Delegate's function of function: public static int Add (int I1, INT I2) {RETURN I1 I2;} public static int Multiply (int I1, int I2) {RETURN I1 * I2;} Then see the delegate variable as a function name, passed the parameters to the function. Console.writeline (OP1 (100,200)); console.writeLine (OP2 (100, 200)); 2. Event implementation process
code:
Using system;
Class class1
{
Static void main (string [] args)
{
Student S1 = New Student ();
STUDENT S2 = New Student ();
S1.Registerok = new student.deLegateRegisterokevent (student_registerok);
S2.Registerok = new student.deLegateRegisterokevent (student_registerok);
S1.Register ();
S2.Register ();
Console.readline ();
}
Static void student_registerok ()
{
Console.writeline ("Hello");
}
}
Class Student
{
Public delegate void delegateRegisterokevent ();
Public Event DelegateRegisterokevent RegisteroK;
Public String Name;
Public void register ()
{
Console.writeline ("Register Method");
Registerok ();
}
}
In the Student class, first declare delegateRegisterokevent (), then use Event and the delegation type to be used (the DelegateRegisterokevent delegation type to be used) declares that the event registerok (can be seen as a commissioned instance.): Public delegate void delegateRegisterokevent ( Public Event DelegateRegisterokevent RegisteroK; then instantiate the Student class in the main () function, then the S1.Registerok event is delegated to the Student_Registerok method. Adding one or even multiple response methods for an event in the .NET object via " =" (add equal) operators; can also cancel these response methods by very simple "- =" (reduction equivalent) operator . Then, when S1.Register () is called, the event S1.RegisteroK occurs. 3. C # Predefined the event processing method, I think the least understanding is that the event is predefined in C #, so that I will start a fog in the event. After checking some information, it finally understood some, as follows: Eventargs is the base class of classes that contain event data, used to pass the details of the event. EventHandler is a delegate declaration (which is declared in the .NET class library) PUBLIC DELEGATE VOID EventHandler (Object Sender, Eventargs E), all: VoID letter (Object parameter name, Eventargs parameter name); The function can be used as a Click event response method for the Control class. As defined below: Private Void Button1_Click (Object Sender, System.EventArgs E) Parameters Object Sender Represents an object that triggered an event, (in fact, it is the reference to the object, if it is Button1 Click event, Sender is Button1) System.Eventargs E represents the corresponding information of the event, such as the X, Y value, etc. of the mouse. Below we have studied the BUTTON class to see the event declarations, take the Click event as an example. Public Event EventHandler Click; Contemporary EventHandler Type Event Click Private Void Button1_Click (Object Sender, System.EventArgs E) {...} This is a method corresponding to the Button1_Click event. Note The parameters of the method are in line with the signature in the delegate (for both the parameter list). Then how do we connect this method and an event, please see the following code. This.Button1.Click = new system.eventhandler (this.button1_click); (in fact, Button1.Click is the instance event for the system.eventhandler delegated. It is very similar to the delegation to delegate the instance to a method) THIS.BUTTON1_CLICK method is tied. Set to this.button1.click event. 4. The use of the parameters of the event.
code:
Using system;
Class class1
{
Static void main ()
{
Student S1 = New Student (); S1.NAME = "Student1";
STUDENT S2 = New Student ();
S2.NAME = "student2";
S1.Registerok = new student.deLegateRegisterokevent (student_registerok);
S2.Registerok = new student.deLegateRegisterokevent (student_registerok);
/ / When the register method is executed, it triggers the Registerok event.
// Registerok event triggered, then execute a Student_Registerok method
S1.Register ();
S2.Register ();
Console.readline ();
}
Static Void Student_Registerok (Registerokargs E)
{
Console.writeLine (EVENFO);
}
}
Class Student
{
Public Delegate Void DelegateRegisterokevent (Registerokargs E);
Public Event DelegateRegisterokevent RegisteroK;
Public String Name;
Public void register ()
{
Console.writeline ("Register Method");
Registerok (New Registerokargs ("Student Name:" Name);
}
}
Class Registerokargs: Eventargs
{
Public String EventInfo;
Public Registerokargs (String EventInfo): Base ()
{
THIS.EVENTINFO = EventInfo;
}
}
Reprinted to the old Cat: http://www.vikosoft.net/blogview.asp? Logid = 526