Anonymous delegate and Lambda expressions, anonymous lambda expressions

Source: Internet
Author: User

Anonymous delegate and Lambda expressions, anonymous lambda expressions

By using anonymous delegation (anonymous method), programming becomes more flexible. For Delegation and anonymous delegation, see my previous Blog "delegation and anonymous delegation".

The Code is as follows:

static void Main(string[] args){            Worker.TwoNumberHandleMethodDelegate method =   delegate(int a, int b)        {            return a + b;        };           Worker worker = new Worker();            int result = worker.HandleTwoNumber(10, 10,method);            Console.WriteLine(String.Format("Result:{0}", result));           Console.ReadLine();}

The worker of the preceding program calculates the result and returns the result based on the parameter and calculation method specified by Main. The equivalence code can be further simplified as follows:

 static void Main(string[] args)        {            Worker worker = new Worker();            int result = worker.HandleTwoNumber(10, 10, delegate (int a, int b)            {                return a + b;            });            Console.WriteLine(String.Format("Result:{0}", result));            Console.ReadLine();        }

If you have experienced js and jquery development, you may be very friendly. $ ("# Id"). click (function () {...}) is used everywhere in jquery (){.........}).

In C #, the anonymous delegate can be further reduced when used, and the simplified result becomes an expression, called a Lambda expression.

static void Main(string[] args)        {            Worker.TwoNumberHandleMethodDelegate method =   delegate(int a, int b)        {            return a + b;        };}

Lambda expressions:

static void Main(string[] args)        {            Worker.TwoNumberHandleMethodDelegate method =   (a, b)=>        {            return a + b;        };}

C # Lambda expressions are divided into two parts: (a, B) as the parameter list and {...} as the method body.

 static void Main(string[] args)        {            Worker worker = new Worker();            int result = worker.HandleTwoNumber(10, 10, (a, b) =>            {                return a + b;            });            Console.WriteLine(String.Format("Result:{0}", result));            Console.ReadLine();        }

The following method compares with Lambda expression Conversion

Private void A1 () {Console. writeLine (".... ");} // Lambda () => {Console. writeLine (".... ") ;}; // if the method body Code contains only one sentence, you can also save the method body braces () => Console. writeLine (".... ");
Private string A2 (int a, int B) {return String. format ("{0} + {1} = {2}", a, B, a + B);} // the preceding example can be simplified to (a, B) =>{ return String. format ("{0} + {1} = {2}", a, B, a + B);} // because the method body has only one sentence, can be further simplified (a, B) => String. format ("{0} + {1} = {2}", a, B, a + B); // note that return should be removed at this time, C # automatically recognized by the compiler => Return Value
Private string A3 (int a) {return String. format ("{0}", a) ;}// the preceding example can be simplified to (a) => String. format ("{0}", a); // If the parameter list has only one parameter, you can remove the () parameter list, which is further simplified to a => String. format ("{0}", );

Through this article, you may have realized that in C #, lambda expressions do not clearly distinguish between methods, delegates, and anonymous methods, and they can be converted flexibly. In actual development, there are many knowledge points about delegation, such as delegation and events. If time permits, I hope I can write down the commissioned articles.

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.