Delegate:
Function pointer function (C ++). Unlike C ++, function pointers only point to static methods. ,
The so-called delegate is to call the delegate function directly in the delegate method of our definition number instead of calling the preset function method during execution, the subsequent work will become much simpler, and even increase the readability and maintainability of functions.
Example: delegate void eatdelegate (string food) // delegate function,
Concept of delegated chain:
Eatdelegate eatchain;
Eatchain = zs + ls + ww;
Eatchain ("watermelon ");
Here + and-play a significant role.
For example:
Eatchain-/+ = ls; // follow the above.
Anonymous method: it is a new function added in C #2.0.
How to delegate dynamic methods:
The delegate is passed as a parameter of the method ..
Event:
Event Publisher: [Sender .]
Event subscriber: [also called receiver .]
The event program of the relationship between the publisher and the subscriber :?
Class publisher // Press
{
Public delegate void publish (); // declare the proxy (or delegate) required by the event .)
Public event public onpublish; // event Method
Public void issue () // Method for triggering an event
{
If (onpublish! = Null)
{
Console. writeline ("publication ");
Onpublish ();
}
}
}
Class subscriber // subscriber
{
Public void recerve () // define the event handler in the event subscriber.
{
Console. writeline ("the subscriber has received the publication ")
}
}
Class story
{
Static void main ()
{
Publisher pub = new publisher ();
Subscriber zs = new subscriber ();
Pub. onpublish + = new publisher. publish (zs. recerve); // subscribe to an event from the event publisher.
Pub. issue (); // trigger the event
}
}
The object name is used when an event is called;
The class name is used when the delegate is called. [because the delegate is static .]
The event publisher only sends the event to the subscriber of the event.
To declare an event, you must first create a delegate. When declaring a delegate, the void type should be used as the return value.
Naming rules for. net framework events:
Readonly: This member cannot be changed after being assigned a value;
Read-Only value to implement the get method.
Provides a protected method in the class that triggers the event. Name the on plus event and raise the event in the method body;
This article is from the "Li Yuan Cao" blog