C # event is the encapsulation of Delegate. delegate is similar to function pointer.

Source: Internet
Author: User

Since C # hosting Code does not contain pointers, when you want to pass a function as a parameter to another function, you cannot use the normal method, as a proxy (delegate) is created, the proxy in C # is similar to the function pointer. In addition to implementing the function pointer, it has other features.

Let's talk about the function pointer in C ++:

Void printnum (INT num) {cout <num ;}// a simple function for printing numbers

Void (* funptr) (INT );// Declare a function pointer

Funptr = printnum;// Function pointer pointing to function printnum

 

// The following statements are equivalent.

Printnum (8 );

(* Printnum) (8 );

Funptr (8 );

(* Funptr) (8 );

Void fun (INT num, void (* funptr) (INT) {funptr (Num);} // function pointer as a parameter to pass to another function

Fun (8, funptr); // call the above function

C # proxy (delegate)

 

After the pointer is finished, it is similar to delegate:

Void printnum (INT num) {console. Write (Num) ;}// a simple function for printing numbers

Delegate void deprintnum (INT num );//Declaring a proxy is similar to declaring a pointer.In C #, it is similar to a custom type.

Deprintnum de; // defines a theorem variable.

De = printnum ;//Instantiate the variable de, which is equivalent to pointing the pointer variable to a function.It is equivalent to the statement de = new deprintnum (printnum );

De (8); // print the number 8, which is the same as printnum (8 ).

Void printsomething (INT num, deprintnum de) {de (Num) ;}// pass proxy variables as parameters, which is equivalent to passing functions as parameters.

Printsomething (8, de); // call the above function

 

The proxy is similar to the function pointer, but it still has some other features. For example, you can bind multiple functions to one proxy variable.

Assume there is also the void printeno (INT num) {console. Write (Num + 1 );}

De + = printeno; // de-= printnum is to remove the binding with the function printnum.

De (8); // at this time, both printnum (8) and printeno (8) are called );

 

C # event)

 

We all know that C # is very fond of declaring a variable (the standard name is a field) in the class according to the object-oriented idea, but we need to get another attribute to encapsulate it.

After there is a proxy, I naturally want to encapsulate it, so there is an event ):

For example, the proxy is clear above.

Delegate void deprintnum (INT num );

Public event deprintnum eprintnum; // actually declare a proxy variable similar to deprintnum de; it is equivalent to encapsulating de. But de is not used here, similar to directly using attributes without Fields

Eprintnum + = printnum; // only + =,-= can be used in the event. You cannot bind an equal sign directly to the function. You can also bind multiple functions consecutively with ++ =.

Eprintnum (8); // call the event, which is the same as the proxy and is equivalent to a function pointer that indirectly calls the function.

// Supplement: Events and proxies are not very different in use, but they are not the same in Declaration,However, there is another big difference:If eprintnum is declared in class class1, calls like eprintnum (8) can only

This is true in class1. If one class1 cannot be instantiated in other classes, you can only bind the event or remove the function through + =,-=, if the proxy is used, you can instantiate class1 and then directly call the proxy. this may be a benefit of encapsulation. if you don't want another class to call your proxy, but want to bind it to a function, you can encapsulate the proxy as an event.

 

Supplement

 

The most common event in C # is. net automatically generated many events, such as clicking the mouse and so on are all an event, but when we write code, we only need to write the code in the function bound to the event. you don't need to worry about anything else.

In addition, we know that C # does not contain global functions, global variables, and all things are integrated into one class (or struct), but we actually find that enumeration (Enum ), in addition to class, the proxy statement can also be placed in class. in fact, we think of Enum as similar to struct and think of proxy as similar class.

 

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.