Three phases of Delegate evolution in C #

Source: Internet
Author: User

    1. Named functions
    2. Anonymous methods
    3. Lambda expression

A delegate is a type that can store a reference as a function, define a delegate, declare a variable of that delegate type, and then initialize the variable to a function reference that has the same return type and argument list as the delegate, and then use the delegate variable to invoke the function, as if the variable were a function. After you have a variable that references a function, you can also perform an operation that cannot be done in other ways, such as passing a delegate variable as a parameter to a function so that the function can invoke any function it references by using a delegate, and you do not need to know which function to call before running.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacecsharprumenjd{classProgram {Delegate voidProcessdelegate (intnum); Static voidShowdouble (intnum) {num*=2;        Console.WriteLine (num); }        Static voidShowthree (intnum) {num*=3;        Console.WriteLine (num); }        Static voidShowfour (intnum) {num*=4;        Console.WriteLine (num); }        /// <summary>        ///here, the delegate is passed as a parameter to the function,/// </summary>        /// <param name= "Pro" ></param>        /// <param name= "num" ></param>        Static voidExcute (Processdelegate Pro,intnum)        {Pro (num); }        Static voidMain (string[] args) {Processdelegate Pro=NULL; stringinput =Console.ReadLine (); if(Input = ="D") Pro+ = showdouble;//the first method of correlation function            Else if(Input = ="F") Pro= Showfour;//the second method of correlation function            ElsePro=NewProcessdelegate (Showthree);//the third method of correlation functionExcute (Pro,1);        Console.readkey (); }    }}

The code shows three basic ways to delegate association naming methods

where + = can achieve Cumulative association Continuous association effect, that is, multiple calls to + =, the method of the delegate association in order to call all the associated methods;

Prior to C # 2.0, using named methods is the only way to instantiate a delegate. However, in later versions of c#2.0, when the overhead of creating a new method is unnecessary, you are allowed to instantiate the delegate and immediately specify the block of code that the delegate will process when it is called.

These are called anonymous methods,

A LAMBDA expression instantiates a delegate:

Resources:

on the common delegate in C #

Three stages of a delegate evolution 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.