asp.net C # delegate type illustration (1/2)

Source: Internet
Author: User

Delegate is a type in C # that is actually a class that holds a reference to a method. Unlike other classes, the delegate class can have a signature (signature), and it can only hold a reference to the method that matches its signature. The functionality it implements is very similar to the function pointers in C + +. It allows you to pass the method M of Class A to the object of another class B, so that the object of Class B can call this method M. However, compared with function pointers, delegate has many advantages that are not available in function pointers. First, a function pointer can only point to a static function, while delegate can refer to static functions and non-static member functions. When referencing a non-static member function, delegate not only holds a reference to this function's entry pointer, but also holds a reference to the class instance that called the function. Secondly, compared with function pointers, delegate is an object-oriented, type-safe, and reliable controlled (managed) object. That is, runtime can ensure that delegate points to an effective method, and you need not worry that delegate will point to an invalid address or a cross-border address.
A delegate in. NET is similar to a function pointer in C or C + +. Using delegates enables programmers to encapsulate method references within a delegate object. You can then pass the delegate object to code that can invoke the referenced method without having to know at compile time which method will be invoked.   Unlike function pointers in C or C + +, delegates are object-oriented, type-safe, and secure. A delegate declaration defines a type that encapsulates a method with a specific set of parameters and a return type. For static methods, the delegate object encapsulates the method to invoke. For instance methods, the delegate object encapsulates both an instance and a method on that instance.   If you have a delegate object and a set of appropriate parameters, you can invoke the delegate with these parameters. An interesting and useful attribute of a delegate is that it does not know or care about the class of the object it references. Any object can be, except that the parameter type and return type of the method must match the delegate's parameter type and the return type. This makes the delegate perfect for anonymous calls.


[<attrlist>] [AccessModifier] _ [Shadows] Delegate [Sub | function] name [(of Typeparamlist)] [([Parameterlist])] [as type ]


Implementing a delegate is simple, with the following 3 steps to implement a delegate:

1. Declares a delegate object that should have the same parameter and return value type as the method you want to pass.

2. Create the delegate object and pass the function you want to pass in as a parameter.

3. In the place where you want to implement the asynchronous call, the method is invoked by the object created in the previous step.

The following is a simple example:


Resolve with delegate (asynchronous call):

public void SetScore (int value)

{

if (Value > | | | value < 0)

{

Console.Out.WriteLine ("Wrong score");

}

Else

{

Score = value;

if (advisedelegateinstance!= null)

{

Advisedelegateinstance.begininvoke (Score,null,null);

}

}

}

However, we lost the results of the teacher's return, do not know if we have passed.

To obtain results by means of a round-hearing method:

public void SetScore (int value)

{

if (Value > | | | value < 0)

{

Console.Out.WriteLine ("Wrong score");

}

Else

{

Score = value;

if (advisedelegateinstance!= null)

{

IAsyncResult res = Advisedelegateinstance.begininvoke (score,null, NULL);

while (!res.iscompleted) system.threading.thread.sleep (1);

string result = Advisedelegateinstance.endinvoke (res);

Console.Out.WriteLine ("The student receives the result of the teacher return T" +result);

}

}

}

Home 1 2 last page
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.