Further understanding of Delegates in C #

Source: Internet
Author: User
Tags get ip

This article introduces the basic knowledge of the delegation, then further studies the Commission.


Delegate type

In fact, the beginning of the idea that the delegate type is a more difficult to understand the concept, how do not feel that the following "Assembleiphonehandler" is a type.


The code is as follows:


public delegate void Assembleiphonehandler ();

In the normal case, if we are to create a delegate type it should be:


The code is as follows:


public class AssembleIphoneHandler:System.MulticastDelegate

{

}

However, this type of writing is not compiled, and you are prompted not to derive subclasses from "System.MulticastDelegate".

In fact, this is the compiler that made a conversion for us, and when we declare a delegate type using the delegate keyword, the compiler creates a delegate type for us in the same way as the code snippet above.

Knowing these things, the understanding of the delegate type is easier, and the delegate type declared by delegate is a subclass derived from "System.MulticastDelegate".


Establish a chain of trust

Let's take a look at the establishment of the delegate chain and the changes to the invocation list, based on an example from the previous article.


The code is as follows:


Class Program

{

static void Main (string[] args)

{

Apple Apple = new Apple ();

Foxconn Foxconn = new Foxconn ();

Apple.assembleiphonehandler D1, D2, d3, d4 = null;

D1 = new Apple.assembleiphonehandler (Foxconn. Assembleiphone);

D2 = new Apple.assembleiphonehandler (Foxconn. Packiphone);

D3 = new Apple.assembleiphonehandler (Foxconn. Shipiphone);

D4 + = D1;

D4 + = D2;

D4 + = D3;

D4 ();

Console.read ();

}

}

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Further understanding of Delegates in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23130.html






Related content C # get IP and determine if IP is in Interval C # THIS1?? üx?μ????? ó? ¨c# methods for arranging elements in an array a summary of how C # generates DLL files
C #, ASP. NET generic tool class Iswhat? (Can judge numbers, IDs, data types, etc.) C # the Tomato Clock countdown code written in C # implements a method to implement custom timing components in C # by automatically switching different SQLite DLL files under 32-bit, 64-bit systems

Further understanding of Delegates in C #

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.