C # event mechanism summarizes (on)

zhaozj2021-02-16  69

1. Delegation implementation process.

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.

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 of all, the code defines a delegate Mathop, the signature match with two functions add (), and multiply () is the same as the number of parameter types thereof):

Delegate Int Mathop (INT I1, INT I2);

MAIN () China code first declares a variable, and initializes the delegate variable. Note that the parameters when the declaration will use the function name of the function passed, not parentheses:

Mathop op1 = new mathop (add);

(Or for mathop op1 = new mathop (multipleoply);)

Vogue of the function of the delegate passing:

Public Static Int Add (Int I1, INT I2)

{

RETURN I1 I2;

}

Public Static Int Multiply (int I1, INT I2)

{

Return I1 * I2;

}

Then put the delegate variable as a function name, pass the parameters to the function. Console.WriteLine (OP1 (100, 200));

Console.WriteLine (OP2 (100, 200));

2. Event implementation process

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 in the main () function, instantiate the Student class, then the S1.Registerok event commissioned 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.

转载请注明原文地址:https://www.9cbs.com/read-16119.html

New Post(0)