Delegate C # Delegate)

Source: Internet
Author: User

delegate C # the biggest role of the proxy is to bind events of the class to Process Programs .
before calling a function through a proxy, you must first check whether the proxy is empty ( null ). A function can be called unless it is null.

static methods or instance methods can be encapsulated in the proxy instance.
agent three steps:
A . custom proxy class: Delegate int mydelegate ();
B . then instantiate the proxy class: mydelegate d = new mydelegate (myclass. mymethod);
C . finally, call the method through the Instance Object: int ret = D ();

The following is an example.

Using system;
Using system. LINQ;
Using system. text;

Namespace consoleapplication1
// Declare delegate, define required signature.
Delegate void delegate (string message );

Class mainclass
// Regular method that macthes signature.
Static void delegatemethod (string message)
Console. writeline (Message );

static void main (string [] ARGs)
{< br> // instantiate delegate with named method.
delegate d1 = new delegate (delegatemethod);
// instantiate delegate with anonymous method.
delegate D2 = new delegate (string message) {console. writeline (Message) ;});

// invoke delegate D1;
d1 ("hello ");
// invoke delegate D2;
D2 ("world ");
}< BR >}< br> to be continued.

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: 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.