1. Introduction
1. "Lambda Expression" is a special anonymous function that simplifies the use of anonymous delegation and is an efficient expression similar to functional programming. Lambda simplifies the amount of code that needs to be written in development.
2. It can contain expressions and statements, and can be used to create delegate or expression directory tree types, supporting inline expressions with input parameters that can be bound to the delegate or expression tree.
3. All Lambda expressions use the Lambda operator =>, which is read as "goes to". On the left side of the Lambda operator are the input parameters (if any), and on the right side are the expression or statement blocks. The Lambda expression x => x * x is read as "x goes to x times x".
4. Lambda expression syntax:
- No parameters: () =>expr
- One parameter: param=>expr
- Multiple parameters: (param-list) =>expr
Note: If there is only one sentence of code in the method body, the code block symbol can be omitted, that is, there is no need to write { }
2. Give examples:
1. Delegate instance method
//Trust to visit the supermarket delegate int GuangChaoshi(int a); static void Main(string[] args) { GuangChaoshi gwl = JieZhang; (gwl(10) + ""); //Print 20, commissioned application (); } //Bill, please public static int JieZhang(int a) { return a + 10; }
2. Lambda expression
//Trust to visit the supermarket delegate int GuangChaoshi(int a); static void Main(string[] args) { // GuangChaoshi gwl = JieZhang; GuangChaoshi gwl = p => p + 10 ; ( gwl(10) + ""); //Print 20, the application of expression (); }
3. Multiple parameters
//Trust to visit the supermarket delegate int GuangChaoshi(int a,int b); static void Main(string[] args) { GuangChaoshi gwl = (p,z) => z-(p + 10); (gwl(10,100) + ""); //Print 80, z corresponds to parameter b, p corresponds to parameter a (); }
3. Built-in delegation
1. Collection operation
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 + "son", Age = 8 - i, }; (p); } return persons; } static void Main(string[] args) { List<Person> persons = PersonsList(); persons = (p => > 6).ToList(); //All Age>6 Person collections Person per = (p => == 1); // Age=1 single people class persons = (p => ("son")).ToList(); //All Names contain the son's Person's collection } }
2. Func<T>Trust
static void Main(string[] args) { Func<int, int, bool> gwl = (p, j) => { if (p + j == 10) { return true; } return false; }; (gwl(5,5) + ""); //Print ‘True’, z corresponds to parameter b, p corresponds to parameter a (); }
4. Dynamic creation method of lambda expression tree
static void Main(string[] args) { //i*j+w*x ParameterExpression a = (typeof(int),"i"); //Create a parameter in the expression tree, as a node, here is the lowest node ParameterExpression b = (typeof(int),"j"); BinaryExpression be = (a,b); // Here i*j generates a node in the expression tree, which is one level higher than the above node ParameterExpression c = (typeof(int), "w"); ParameterExpression d = (typeof(int), "x"); BinaryExpression be1 = (c, d); BinaryExpression su = (be,be1); //Operate two intermediate nodes to generate endpoint Expression<Func<int, int, int, int, int>> lambda = <Func<int, int, int, int, int>>(su,a,b,c,d); (lambda + ""); //Print ‘(i,j,w,x)=>((i*j)+(w*x))', z corresponds to parameter b, p corresponds to parameter a Func<int, int, int, int, int> f= (); //Compile the lambda expression described by the expression tree into executable code and generate the delegate of the lambda expression; (f(1, 1, 1, 1) + ""); //Print 2 (); }
This is all about this article about Lambda expressions in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.