Agents in C # (delegate)

Source: Internet
Author: User
Tags empty function prototype instance method
Some books are also referred to as "representatives" or "commissions".
The concept of a pointer is canceled in C #. There are two ways to solve a programmer who is attached to a pointer: Declare a "unsafe" code snippet and then use a pointer in it or use a reference type of C #-"Agent" (delegate). The "Agent" is equivalent to the function pointer prototype in C, except that C # is type-safe.

Delegate (delegates, delegates): "Agent" is type-safe and fully object-oriented.
(1) In C #, all proxies are derived from the System.Delegate class (delegate is a system.delegate alias).
(2) The agent implicitly has the sealed property, that is, it cannot be used to derive the new type.
(3) The most important role of the agent is to bind event handlers for the event of the class.
(4) before calling a function through a proxy, you must first check whether the proxy is empty (null), or not empty, to invoke the function. (5) A static method can be encapsulated in a proxy instance or an instance method can be encapsulated.
(6) When creating a proxy instance, you need to pass the method or other proxy instance that will be mapped to indicate the function prototype that the agent will encapsulate (. NET is called the method signature: signature). Note that if a static method is mapped, the parameter passed should be the class name. Method name, if the mapping is an instance method, the passed parameter should be the instance name. Method Name.
(7) Only if the two proxy instances are mapped by a method and the object to which the method belongs is considered to be the same (considered from the function address).
(8) Multiple proxy instances can form a chain of agents, the static method for maintaining the agent chain is defined in the System.Delegate combion,remove, and the agent instance is added to the agent chain and the proxy instance is deleted.
(9) The definition of an agent must be placed outside of any class, such as delegate int mydelegate (), and the mydelegate d = new MyDelegate (Myclass.mymethod) is invoked in the class's method to instantiate an instance of the custom proxy.
(10) Three-step agent:
A To generate a custom proxy class: Delegate int mydelegate ();
b Then instantiate the proxy class: MyDelegate d = new MyDelegate (Myclass.mymethod);
C Finally, the method is invoked through an instance object: int ret = d ();

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.