C # delegation and method learning experience

Source: Internet
Author: User

Recently, I have been thinking about the delegate and event mechanisms in C #. I suddenly think that the delegate and time mechanisms in C # are actually the same as the function pointers in C ++, probably because

C # has no pointer, so we have developed a mechanism that supports delegation and events. In C #, the method is encapsulated as a special object, so that the method name is used as a variable and assigned a value.

Operation. Define the "function pointer" in C # To use the delegate keyword for modification. Then, you can use the "function pointer" to define an object or an array of objects and assign values. After the value assignment operation is complete, you can directly

Use the corresponding function name, for example:


---------------------------- YYC


Public delegate int Func ();

Static int fun1 ()
{
Return 1;
}

Static int fun2 ()
{
Return 2;
}

Func fc;
Fc = fun1;
MessageBox. Show ("fun1:" + fc (); // output 1

Fc = fun2;
MessageBox. Show ("fun2:" + fc (); // output 2

C # estimates the storage structure of a queue for the delegate object. Therefore, the delegate object can be added or subtracted,

For example:

Public delegate int Func ();

Static int fun1 ()
{
MessageBox. Show ("fun1 :");

Return 1;
}

Static int fun2 ()
{
MessageBox. Show ("fun2 :");
Return 2;
}

Func fc;
Fc = (Func) fun1 + (Func) fun2;

Fc ();

When function fun1 () and function fun2 () are called again, they are executed according to the loading order.

However, you should note that the delegate object can be added or subtracted, So if you use any exception, you should first determine whether the delegate is empty.

Of course, in C #, apart from delegation, another important mechanism is the release of events and events. The main purpose of the event type in C # is

Prevent the programmer from being negligent or maliciously used = directly assign values to the entrusted object, thus damaging the delegate loading. Therefore, the function pointer setting occurs.

You must use the event keyword to modify a semantic object.

At the same time, in order to reduce repetitive work and improve efficiency in the Time Release and subscription process of C #, C # has defined the EventHandler delegate type for us.

Publuc delegate void EventHandler (oblect sender, EventArgs e );

The object class is the base class of all classes. The object used to pass events is the publisher. If the publisher is a static class, null is passed directly. Generally, you can use this directly in the publisher class.

Note that the data is packed during transmission. After receiving the object, you must perform the corresponding unpacking operation.

EventArgs time data class is mainly used to transmit event data, as long as the data inherits EventArgs, it can be transmitted.

To put it bluntly, the delegate mechanism in C # is the same as the function pointer in C ++ and can be thought like this. Later, the so-called subscriber and publisher Mechanism

It is also based on this principle.


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.