C # study notes -- 2

Source: Internet
Author: User

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

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.