Lambda expressions Detailed
Foreword
1, so hot, the programmer is not easy to live, Sunday, also braved the burning sun, summed up these things.
2, qwaqwa lambda: simplifies the use of anonymous delegates, so that you make the code more simple and elegant. It is said that it is one of Microsoft's new since the c # 1.0 of the most important functions.
lambda Profile
lambda operator: All lambda expressions are with a new lambda operator "=>", you can call him, "Go" or "become." Operator of the expression is divided into two portions, the input parameters specified on the left, the right is the subject of lambda.
lambda expression:
1. a parameter: param => expr
2. plurality of parameters: (param-list) => expr
Above these things, remember, let's start the application and elaborate lambda, let you enjoy it.
lambda elaborate application
Illustrate this technique, let me one example, and then slowly in-depth analysis. Examples are as follows:
namespace 阐述lambda
{
public class Person
{
public string Name { get; set; } public int Age { get;set; } } class Program { public static List<Person> PersonsList() { List<Person> persons = new List<Person>(); for (int i = 0; i < 7; i++) { Person p = new Person() { Name = i + "儿子", Age = 8 - i, }; persons.Add(p); } return persons; } static void Main(String [] args) {List <the Person> persons = PersonsList (); persons.Where persons = (P => p.Age> 6) .ToList (); // All Age> Person 6 Person per = persons set .SingleOrDefault (P => == p.Age . 1); // Age persons 1 = single class of people persons.Where = (P => p.Name.Contains ( " son .")) ToList (); // Person Name contains a collection of all the sons }}}
Kanla example above, I believe you can see that it is indeed a sweet dates, Oh, let's look at (p => p.Age> 6) such expressions, in the end how it was. .
First, we look commissioned
// delegate the supermarket
the delegate int GuangChaoshi ( int A); static void the Main ( String [] args) {= GWL GuangChaoshi JieZhang; Console.WriteLine (GWL ( 10) + ""); // print 20, delegate application Console .ReadKey ();} // checkout public static int JieZhang ( int A) { return A + 10 ;}
Look at the expression
// delegate the supermarket
the delegate int GuangChaoshi ( int A); static void the Main ( String [] args) { // GuangChaoshi = GWL JieZhang; GuangChaoshi GWL = P => P + 10 ; Console.WriteLine (GWL ( 10) + " "); // print 20, the application of the expression Console.ReadKey ();}
Entrusted with the expression of two pieces of code, we can see some stuff it: in fact, the expression (p => p + 10; ) in the parameter p represents the delegate method, and the right of expression symbol p + 10 is commissioned method returns the result. Heroes pass, the next shrimp understand.
The following two further understanding appreciated slightly more complex.
1. Multi-parameter
// delegate the supermarket
the delegate int GuangChaoshi ( int A, int B); static void the Main ( String [] args) {GuangChaoshi GWL = (P, Z) => Z- (P + 10 ); Console.WriteLine (GWL ( 10, 100) + ""); // print 80, z corresponds to the parameter b, p corresponding to the parameter A the Console.ReadKey ();}
2. lambda body computational complexity
/// <summary>
/// 委托 逛超市
/// </summary> /// <param name="a">花费</param> /// <param name="b">付钱</param> /// <returns>找零</returns> delegate int GuangChaoshi(int a,int b); static void Main(string[] args) { GuangChaoshi gwl = (p, z) => { int zuidixiaofei = 10; if (p < zuidixiaofei) { return 100; } else { return z - p - 10;}}; Console.WriteLine (GWL ( 10, 100) + ""); // print 80, z corresponds to the parameter b, p corresponding to the parameter A the Console.ReadKey ();}
These examples above, under good understanding, here I want to introduce a system specified Fun <T> delegate.
Func <T> delegate
T is the parameter type, which is a generic type of commission, use them very convenient.
The first example
static void the Main ( String [] args) { Func < int, String> GWL = P => P + 10 + " - return type String " ; Console.WriteLine (GWL ( 10) + ""); // Print '20 - return type string ', z corresponding to parameters b, p corresponding to the parameter A the Console.ReadKey ();}
Description: We can see, p here is int type parameters, however lambda body returns type string.
Then the previous example
static void the Main ( String [] args) { Func < int, int, BOOL> GWL = (P, J) => { IF (P + J == 10 ) { return to true ;} return to false ;}; Console.WriteLine (GWL ( . 5, . 5) + ""); // Print 'True', z corresponding to parameters b, p corresponding to the parameter A the Console.ReadKey ();}
Note: From this example we can see, p is an int, j is of type int, bool return value type.
After reading the above two examples, I believe we should understand that it Func <T> Usage: a plurality of parameters, the previous method of parameters for the commission, the last parameter, return type of the delegate method.
lambda expression trees dynamically created method
static void Main(string[] args)
{
//i*j+w*x ParameterExpression a = Expression.Parameter(typeof(int),"i"); //创建一个表达式树中的参数,作为一个节点,这里是最下层的节点 ParameterExpression b = Expression.Parameter(typeof(int),"j"); BinaryExpression be = Expression.Multiply(a,b); //这里i*j,生成表达式树中的一个节点,比上面节点高一级 ParameterExpression 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); //运算两个中级节点,产生终结点 Expression<Func<int, int, int, int, int>> lambda = Expression.Lambda<Func<int, int, int, int, int>>(su,a,b,c,d); Console.WriteLine(lambda + ""); //打印‘(i,j,w,x)=>((i*j)+(w*x))’,z对应参数b,p对应参数a Func<int, int, int, int, int> f= lambda.Compile(); //将表达式树描述的lambda表达式,编译为可执行代码,并生成该lambda表达式的委托; Console.WriteLine(f(1, 1, 1, 1) + ""); // Print 2 the Console.ReadKey ();}
This code, impress, under careful understanding, a thorough understanding of the matter, lambda expression is basically no friends. Ha ha. .
Forget it, I draw a map be the end of it, in order to facilitate understanding.
lambda expression trees on code segments, FIG.