CLR notes: 15. Delegate

Source: Internet
Author: User

1.Delegate is type-safe, that is, errors can be detected at compile time, and similar reflection are of type unsafe.

Delegate are pointers to method addresses and do not distinguish between static and instance methods.

Delegate is defined outside of class, and this peer class includes the method that delegate to use.

2.Delegate allows covariant (covariance) and anti-covariance (contra-variance) of reference types. That

Delegate Object Mycallback (FileStream fs);
String Someothermethod (Stream s)

{
}

You can bind the Someothermethod method to a Mycallback delegate.

Covariance means that for a return type, a method can be derived from a delegate.

"Anti-covariance" means that for a parameter type, a delegate can be derived from a method.

3. The method callback of a delegate is generally written like this:

void CallBack (String p1, String p2, Testdele D)

{

if (d!= null)

{

D (P1, p2);

}

}

Here's a piece of crap: Using the callback method, you can pass in a null value to the Testdele parameter; In the callback method, if you want to determine if the D value is NULL, NULL cannot execute the method.

4.Delegate Essence: A class

The following delegate declares:

Public delegate string Testdele (int inttest);

Equivalent to this class:

public class TestDele:System.MulticastDelete

{public
testdele (int inttest, IntPtr method);        Public 
virtual String Invoke (int inttest);
Public virtual IAsyncResult beginresult (int inttest, AsyncCallback callback, 

object);
public virtual void EndInvoke (IAsyncResult result);

 }

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.