What is the difference between the delegate callback static method and the instance method?

Source: Internet
Author: User

Analyze problems

Before analyzing the internal structure of the Delegate, let's review the most basic concepts. In C #, what are the differences between static and instance methods. Similar to most other advanced object-oriented languages, static methods are defined by the keyword static in C #. Static methods can be accessed by class names without any instance objects, in a static method, no non-static members of the access type are allowed. The instance method needs to be called through a specific instance object to access any member of the Instance Object.

Now let's take a look at the differences between the delegated instance binding method and the static method. As mentioned above, when an instance method is called, it needs to be accessed through the instance object. As you can imagine, to bind an instance method to a delegate, you must obtain the code segment of the instance method and the information of the instance object at the same time, so that when the delegate is called back.. net. Shows the internal structure of the delegate.

 

_ Target is a reference to the target instance. When an instance method is bound to a delegate, this parameter is set to an instance object of the type of the method. When a static method is bound to the delegate, this parameter is set to null.

_ Methodptr is a pointer to the code snippet of the binding method, which is very similar to the function pointer in C ++. Binding static methods or instance methods does not differ in the settings of this Member.

In fact. for the custom delegate type of multicastdelegate, there is another member variable: _ Prev, which points to the next delegate in the delegate chain. The concept of chain delegate will be overwritten in the later lady. So far, the reader has understood the difference between the delegated instance binding method and the static method, that is, the member settings of _ target.

Answer

When the delegate is bound to a static method, the internal object member Variable _ target is set to null. When the delegate is bound to an instance method, _ target will be set to an instance object pointing to the type of the instance method. When the delegate is executed, the instance of this object will be used to call the instance method.

What is the difference between the delegate callback static method and the instance method?

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.