C # event mechanism induction (I)

Source: Internet
Author: User

1. Delegation implementation process.

First, let's take a look at the delegation. Delegation is actually the transfer of methods and does not define the implementation of methods. An event is actually a standardized assignment, specially crafted and slightly specialized multicast assignment (multi-point assignment) for the event handling process ). The following is an example. I think it is easier to compare the delegate example with the event example.

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. The signature matches the two functions Add () and Multiply () Signatures (that is, they carry the same number of parameter types ):


Delegate int MathOp (int i1, int i2 );

In Main (), the code first declares a variable using the new delegate type and initializes the delegate variable. note that when declaring a parameter, you only need to use the name of the function passed by the delegate without brackets:

MathOp op1 = new MathOp (Add );

(Or MathOp op1 = new MathOp (Multiply );)
 
Function body of the function passed by the delegate:

Public static int Add (int i1, int i2)

{

Return i1 + i2;

}

Public static int Multiply (int i1, int i2)

{

Return i1 * i2;

}

Then, the delegate variable is treated as a function name and the parameter is passed 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, declare the delegate DelegateRegisterOkEvent (), and then use the event and the delegate type to be used (the previously defined delegate type of DelegateRegisterOkEvent) declare the event RegisterOK (it can be considered as an instance of the Delegate .) :

Public delegate void DelegateRegisterOkEvent ();

Public event DelegateRegisterOkEvent RegisterOK;

The Student class is instantiated in the Main () function, and the s1.RegisterOK event is delegated to the Student_RegisterOK method. It is very easy to use the "+ =" (plus or equal) operator. add one or more response methods to an event in the. Net object. You can also cancel these response methods by using the very simple "-=" (minus or equal) operator.

Then, when s1.Register () is called, the event s1.RegisterOK occurs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.