Learning Lamda Expressions

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Linq.Expressions;namespaceElaboration lambda{classProgram {Delegate intGuangchaoshi (inta); /// <summary>        ///Entrust a supermarket shopping/// </summary>        /// <param name= "a" >spend</param>        /// <param name= "B" >Pay Money</param>        /// <returns> Change</returns>        Delegate intGuangChaoshi2 (intAintb); //Checkout         Public Static intJiezhang (inta) {returnA +Ten; }         Public classPerson { Public stringName {Get;Set; }  Public intAge {Get;Set; } }         Public StaticList<person>personslist () {List<Person> persons =NewList<person>();  for(inti =0; I <7; i++) {person P=NewPerson () {Name = i +"son", age =8-I,}; Persons.            ADD (P); }            returnpersons; }        Static voidMain (string[] args) {List<Person> persons =personslist (); Persons= persons. Where (p = p.age >6). ToList ();//a collection of all age>6Person per = persons. Singleordefault (p = p.age = =1);//single people class for age=1persons = persons. Where (p = p.name.contains ("son")). ToList ();//all name contains the collection of the son's person//1Guangchaoshi GWL =Jiezhang; Console.WriteLine (GWL (Ten) +"");//Print 20, the application of the delegateConsole.readkey (); //2Guangchaoshi gw2 = p + p +Ten; Console.WriteLine (GW2 (Ten) +"");//Printing 20, application of expressionsConsole.readkey (); //3GuangChaoshi2 gw3 = (p, z) = =            {                intZuidixiaofei =Ten; if(P <Zuidixiaofei) {                    return  -; }                Else                {                    returnZ-p-Ten;            }            }; Console.WriteLine (GW3 (Ten, -) +"");//print 80,z corresponding parameter b,p aConsole.readkey (); //4//i*j+w*xParameterExpression A = Expression.parameter (typeof(int),"I");//Create a parameter in an expression tree, as a node, this is the lowest nodeParameterExpression B = Expression.parameter (typeof(int),"J"); Binaryexpression be= Expression.multiply (A, b);//here i*j, generate a node in the expression tree, one level higher than the above nodeparameterexpression C= Expression.parameter (typeof(int),"W"); ParameterExpression D= Expression.parameter (typeof(int),"x"); Binaryexpression BE1=expression.multiply (c, D); Binaryexpression su= Expression.add (Be, BE1);//operation of two intermediate nodes, generating endpointsExpression<Func<int,int,int,int,int>> lambda = expression.lambda<func<int,int,int,int,int>>(Su, a, B, C, D); Console.WriteLine (Lambda+"");//print ' (i,j,w,x) = ((i*j) + (w*x)) ', z corresponding parameter b,p corresponding parameter aFunc<int,int,int,int,int> f = lambda.compile ();//The lambda expression described by the expression tree is compiled into executable code and the delegate of the lambda expression is generated;Console.WriteLine (F (1,1,1,1) +"");//Printing 2Console.readkey (); //5func<int,string> gw5 = p + p +Ten+"--The return type is String"; Console.WriteLine (Gw5 (Ten) +"");//print ' 20--return type is String ', Z corresponds to parameter b,p aConsole.readkey (); //6func<int,int,BOOL> gw6 = (p, j) = =            {                if(p + j = =Ten)                {                    return true; }                return false;            }; Console.WriteLine (GW6 (5,5) +"");//print ' True ', z corresponds to parameter b,p aConsole.readkey (); }    }}

Learning Lamda Expressions

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.